File Written in Chinese (I think?)

user3258699

I have an assignment to create my own version of UNIX ar, in C. I broke the assignment up into several pieces and I am just trying to write the file names into another file(which will be my archive file) at this point. I can create the file, and when I use printfs to make sure I'm getting the proper arguments from the command line, the file names are correct. When I open the file that I have created that is suppose to hold those file names, everything is in an Asian language.

I've researched the problem and haven't found much to go on. The only thing I did find was something about the files being encoded differently, but I'm pretty confused at this point.

#include <stdlib.h>
#include "ar.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main (int argc, char **argv)
{
    char *file_name, *archive_name;
    size_t count = 50;
    int i, fd, num_written;

    //command line options to do later
    //argv[1]

    //get archive name
    archive_name = argv[2];

    //get file names to add to archive
    for (i = 3; i<argc; i++)
    {
        file_name = argv[i];
    }

    //create archive file
    fd = open(archive_name, O_RDWR |O_CREAT|O_APPEND);
    if (fd < 0){
        perror("Error while opening file.\n");
        exit(EXIT_FAILURE);
        }
    else
        printf("File %s opened successfully\n", archive_name);


    //write file names into archive file
        for (i = 3; i<argc; i++)
    {
        printf("file name: %s\n", argv[i]);
        num_written = write(fd, argv[i], 50);
        if (num_written == -1){
            perror("Error while writing to archive.\n");
            exit(EXIT_FAILURE);
            }

     }


    close(fd);

    return 0;

    }
Mooing Duck

Your code is assuming all arguments after the archive name are 50 bytes long. If the arguments are shorter than that, the program may crash or write random data. Since it didn't crash, and Notepad (or whatever) seems to think it's Chinese, I'm going to assume it wrote the random data. Use strlen to figure out how long each of the argument names are.

Additionally:
you check argv[2] without checking argv, which could cause a crash.
Nothing you do with file_name makes any sense.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Text written in Chinese/Japanese using fstream and not reading file correctly

I have written a path tracer using julia programming language but i think it is slow

Api call works with curl but not with google apps script, I think the header is written wrong

I think I have problem with compiling a java file

Why the file is not written when I did fwrite() to it?

Java file I/O problem: file now written by newBufferedWriter

In bash, how can I delete a file, whose name was written in a file?

Xcode error(I think)?

Java I/O problem the string was written but didn't show on the file

Why can I successfully move a file in Linux while it is being written to?

how do I check whether a plot was written to file in R

How can I format the data written to a text file to be done in columns?

How do I analyze JavaScript code written in .php file in SonarQube

How can I know if a variable can be written directly into a binary file?

How can I delete the last word written in the text file

How would I change this tuple back into string to be written in a file?

How can I use a newly written value in a required file?

Why does Rome think my file is empty when I try to compile it?

Why does Rome think my file is empty when I try to compile it?

How can I make Windows think a file "came from another computer"?

How to fix (what I think is) an encoding issue when exporting python dictionary to .csv file using pandas DataFrame?

Read/write program in c that copies file and i think its taking too long to copy

Compile error in my parser, think I have my input file wrong, but unsure what did wrong

ls -a lists files with "->" arrow I think they are linked how can I make my new file to change when referenced file is changed?

Port forwarding issue (I think)

Boost::Regex Segfault (I think)

I think MySqli select is not working?

I think my ssd is bad

Scope (I think?!) challenge in JavaScript

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive