-1

Here's a snippet of the code:

FILE *fp;
fp = fopen(argv[2], "r"); // the input file
while (!(feof(fp))) {
    char c;
    int i = 0;
    char string[101]; // max string length is 100. last spot reserved for '\0'
    string[100] = '\0';
    while ((c = fgetc(fp)) != ' ' && i < 100) {
        string[i] = c;
        i++;
    }
    printf("%s\n",string);
}
fclose(fp);

This is just an example of a bigger problem that I'm having, but I feel like if I understand why this is happening.

The contents of the "argv[2]" file are as follows:

this is an input file

The output I am getting overall by this is:

this
isis
anis
input
file
�����������������������������������������������������������������������������������������������

...as opposed to what I"m looking for:

this
is
an
input
file

I'm confused as to why the string doesn't seem to be a new string (i.e., why the second time around the string is "is" followed by the remaining two characters of the last string which also happen to be "is" making "isis" all together). Why doesn't the same thing happen to the last two? Why isn't "file" printed as "filet"? Very confusing.

Overall I'm just trying to get my desired output. Any suggestions as to why I'm not? Why is the string being overridden as opposed to the program making a new string for each iteration?

Hello
  • 219
  • 2
  • 7
  • You are trying to use the format specifier %s for character arrays that fo not contain strings. And using the condition !feof( fp ) is not correct. – Vlad from Moscow Sep 21 '17 at 17:31
  • 1
    You should be checking for EOF after *each* character reading. – Eugene Sh. Sep 21 '17 at 17:31
  • When you update a character in your `string`, we're you expecting the other characters to magically disappear? If this were to happen, why would you expect a whole world to be in your string? – alvits Sep 21 '17 at 17:35
  • 2
    On a related note, using `feof()` in a loop control is almost always wrong. For more information: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Robᵩ Sep 21 '17 at 17:36
  • There are *many errors* here; it's indicative of a much larger problem: **Which book are you reading?** Whatever it is, it's not working for you. 1/ check return value of `fopen`. 2/ `feof` call should occur *after* attempts to read have failed (which implies that you also need to *determine when attempts to read have failed*, which implies...) 3/ use `int` to store the return value of `fgetc` (and don't argue with me on this matter until you've picked up a more reputable book) and `size_t` for array indexes (that's less serious), and 4/ terminate *at the end of the string*, **not after**! – autistic Sep 21 '17 at 18:24
  • Possible duplicate of [fgetc reads character with value = -1](https://stackoverflow.com/questions/34245695/fgetc-reads-character-with-value-1) – autistic Sep 21 '17 at 18:29
  • 1
    1. `fgetc()` returns `int`, not `char`. Using a `char` to store the return value from `fgetc()` is *wrong*. 2. As others have noted, read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Andrew Henle Sep 21 '17 at 18:51
  • A catch-all approach is to just initialise the whole string to all `0`s: `char string[101] = {0};` – alk Sep 22 '17 at 15:20

3 Answers3

1

You need to add a null-terminator to the end of the string. After you exit the while loop, set string[i] = '\0'. Do some research to learn about strings in C and null terminators ('\0'); that's something you'll need to learn.

D.W.
  • 3,382
  • 7
  • 44
  • 110
1

You are putting the null terminator (\0) at the last index of the character array string, not at the logical end of the string. That's why you see the junk characters getting printed. There is also a flaw in your logic of the loops. I suggest the an alternative, sweet and succinct. Just use fscanf since you want to read each word, not character from the file.

char string[101];
while (fscanf(fp, " %100s", string) == 1) {
    printf("%s\n",string);
}
VHS
  • 9,534
  • 3
  • 19
  • 43
0

Your string array is 101 characters long and has a NULL terminator at the very end of the array. Now you are reading from a file to the array and filling it. However it seems like you are reading far less then 100 characters. Now when you try to print the string, the unfilled data is random. Chances are high that there could be a null character somewhere in there and you wont see the junk, but as you see yourself it's not the case. As others suggested put the '\0' directly after the end of the characters you have read so you wont be printing the full array but only the part you want to see.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Murad Babayev
  • 115
  • 2
  • 6