Arturs Vancans
Arturs Vancans

Reputation: 4640

After using fopen to open a text file in C, it has additional characters

I need to read in table of data in a format x*[tab]*y*[tab]*z*[tab]\n* so I am using fopen and fgetc to stream characters. Loop is ending when c==EOF. (c is character.) But I had difficulties with that as it overflows my array. After doing some debugging I realised that the opened file after the last line contains:

Northampton Oxford 68 ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ[...]ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««îþîþ

What is that? And why does that not appear in my plain text file? And how do I overcome this problem?

destination = fopen("ukcities.txt", "rt"); // r = read, t=text 

if (destination != NULL) {
    do {
       c = fgetc (destination);
              if (c == '    ') {
                temp_input[i][n] = '\0';
                i++;
                n=0;
              } else if (c == '\n') {
                  temp_input[i][n] = '\0';
                  printf("%s %s %s \n", temp_input[0], temp_input[1], temp_input[2]);
                  i = 0;
                  n=0;
              } else {
                  temp_input[i][n] = c;
                  n++;
              }
        } while (c != -1);  

    return 1;
} else {
    return 0;       
}

Upvotes: 0

Views: 786

Answers (2)

dreamlax
dreamlax

Reputation: 95355

That string looks unterminated. In C, strings that don't end with a '\0' character (a.k.a. null character) lead to constant trouble because a lot of the standard library and system libraries expect strings to be null-terminated.

Make sure that when you have finished reading in all the data, that the string is terminated; in some cases it must be done manually. There are a few ways to do this (the below makes all characters of the string null, so as long as you don't overwrite the very last one, the string will always be null terminated):

// (1) declare an array of char, set all characters to null character
char buffer[1000] = {0};

Alternatively, if you are keeping track of where you are in the buffer, you can also do this:

// (2) after reading in all data, add the null character yourself:
int n; // number of bytes read
char buf[1000];

// read data into buf, updating n

buf[n] = '\0'; // (tip: may need to use buf[n+1])

In either case, it is important that you don't overstep the end of the buffer. If you've only allocated 1000 bytes, then use only 999 bytes and save 1 byte for the null character.

Upvotes: 0

Seth Carnegie
Seth Carnegie

Reputation: 75150

Looking into my crystal ball, I see that fread or whatever you're using (apparently that's fgetc which makes it even more true) doesn't null-terminate the data it reads and you're trying to print it as a C-string. Terminate the data with a NUL character (a 0) and then it will print correctly.

Upvotes: 2

Related Questions