Thread: Why is my C program printing like this? Do I not understand how strings work?

  1. #16
    Banned
    Join Date
    Aug 2017
    Posts
    861
    furthermore, the actual problem was that the OP was NOT checking for EOF at all. a file under 100 is going to produce what kind of results if it just keeps reading "empty space"?
    Code:
    FILE *fp;
        fp = fopen(argv[1], "r"); // the input file
        while (!(feof(fp))) {
            //char c;
            int 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);
    
        return 0;
        }
    so basically that is the issue here, how it gets done. It looks like their is more then one solution as long as the check for End Of File is included into it while reading the file. Because that is how it was designed to be. as even the feof shows that the End Of File is still being used to give indication that it has read the end of the file, stop trying to read it.

    where if the person is using a small buffer size then that check too needs to be placed into the equation along with EOF.

    another example by someone else using this buffer check method. without using that you're saying has to be else it is wrong.

    C Tutorial – File I/O (using text files) | CodingUnit Programming Tutorials
    Code:
    while (fgets(buf,1000, ptr_file)!=NULL)
            printf("%s",buf);

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I don't think that's what was being argued. Where and how you check for EOF, everyone agrees as long as you do it before you process input, there should be no problem.

    What you were being corrected on was assigning fgetc()'s return value to char, and indeed, no other examples except for code that you wrote does this.

    EOF is represented as an int. Here is chapter and verse on it:

    Quote Originally Posted by C11 7.21.3
    EOF which expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream
    So, since fgetc() potentially returns EOF, the return value has to also be stored as an integer to properly represent EOF. The rest of the discussion before now was tailored to proving that.

    *Just a quick note that I quoted a working paper, so subsections may have moved around.

  3. #18
    Banned
    Join Date
    Aug 2017
    Posts
    861
    OOhhh I was keeping the data type char and not changing the return to int? why didn't he just say that? it would have stopped a lot of ho ha in this post.

    now I got a go back and look at what I did. thanks for that possible clarification

  4. #19
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by userxbw View Post
    now you tell me that example is wrong by now stating I and others should not use that

    Where have I told you that this is wrong???


    Quote Originally Posted by userxbw View Post
    which equates to the same as this,
    Code:
    while ( c = fgetc(pfile) != EOF ) {
        if (c == '$')
        printf (" got it ($)\n");
    }
    which you used to show this is how to do it, now you're saying it is not. Why are you now contradicting yourself, for one?

    I dont know where you find a contradiction. Both examples uses 'fgetc' and both assign the returned value to a int-variable.
    BTW, you should set the assignment in perentheses, so that the compiler knows what operation comes first.
    Code:
    while ((c = fgetc(pfile)) != EOF) {

    Quote Originally Posted by userxbw View Post
    where many others too will contradict what you just stated to me,
    file - issue fgetc() c using while loop - Stack Overflow

    The problem in this thread has nothing to do with 'fgetc'.
    There is no contradiction because nobody use 'feof' there and the returned value of 'fgetc' will be assigned to a int-variable.


    Quote Originally Posted by userxbw View Post
    Code:
    char buffer[500];
    FILE *fp;while (!feof(fp))....
    reference:
    http://people.cs.uchicago.edu/~dmfra...ials/fgets.txt

    Your snippet is to short. Here is the full code
    Code:
    char buffer[500];
    FILE *fp;
    int lineno = 0;
    if ((fp = fopen("myinputfile.txt","r")) == NULL)
    {
    	printf("Could not open myinputfile.txt\n");
    	exit(1);
    }
    
    while ( !feof(fp))
    {
    	// read in the line and make sure it was successful
    	if (fgets(buffer,500,fp) != NULL)
    	{
    		printf("%d: %s",lineno++,buffer);
    	}
    }
    You can see in this reference that the autor uses 'fgets' (and not 'fgetc') and he/she dont use it blindly.
    The 'fgets'-call is encapsulated in a if-contition. This means that the result will only be used if it is valid.
    You can also use 'feof' as your while-contition if you use 'fgetc' inside the loop-body. But then, you should also check the returned value.
    Code:
    // correct 'feof'-variant
     
    while (!(feof(fp))) {
        if ((c = fgetc(fp)) != EOF) {
            string[i++] = c;
        }
    }
    You can also shorten the code from you reference
    Code:
    while (fgets(buffer,500,fp) != NULL)
    {
        printf("%d: %s",lineno++,buffer);
    }
    The point is in this case, that you have a extra check if you use 'feof'.
    Avoiding 'feof' and set the if-contition as while-contition make the code smaller and cleaner (and possibly faster).


    Quote Originally Posted by userxbw View Post
    again these two examples show what I am now suggesting as a truth. it has to be, because both deferent ways are being used to read a file,
    and are considered by the others to be a right way of doing it.

    If you expect errors, 'ferror' is the best way to catch it.


    Quote Originally Posted by userxbw View Post
    Just so you know it looks like you're going to have to inform
    the entire programming community of your findings.

    I have not finding them, it is defined in the C-Standard. Experienced programmer know this allready.


    Quote Originally Posted by userxbw View Post
    It looks like their is more then one solution as long as the check for End Of File is included into it while reading the file.

    Correct! But a programmer should use the simplest way to reach the goal.
    As i show you above, you can use 'feof', but you then need a second check.
    Last edited by WoodSTokk; 09-24-2017 at 09:28 PM.
    Other have classes, we are class

  5. #20
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by WoodSTokk View Post
    Where have I told you that this is wrong???

    .
    to cut this short, I distinctly remember in my searching I actually seen an example of char c being used, and int c being used. it may have or may not have been for fgetc because on my last round in searching then posting to you on this matter I found myself getting fgets and getchar and them other functions that do essentially the same so maybe that is also a cause of my confusion, maybe I do not know because now as murphys law states somewhere I can not find that example on google that mislead me to believing that char c and int c can be used.

    along with when I ran it I seen it giving me a extra "bad" char on output, which I commented on in here.long story short,
    I stand corrected thank you and the other for pointing out the mishap in the simple clear manner to help this situation.

    lesson learned from my mistake .. not yours.

  6. #21
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    No problem, You are welcome.
    I have started to think that my wording was incorrect, because I am not a nativ english speaker.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help printing strings
    By Frendin in forum C Programming
    Replies: 6
    Last Post: 12-09-2012, 07:39 PM
  2. simple printing calculator in C doesn't work
    By Pierre Hardy in forum C Programming
    Replies: 6
    Last Post: 02-10-2012, 02:03 PM
  3. I dont understand why this wouldn't work
    By We'reGeeks in forum C Programming
    Replies: 28
    Last Post: 03-02-2011, 03:26 AM
  4. don't understand why this code doesn't work properly
    By artistunknown in forum C Programming
    Replies: 10
    Last Post: 01-23-2010, 08:48 PM
  5. Don't understand why this doesn't work. . .
    By k2712 in forum C Programming
    Replies: 7
    Last Post: 10-07-2007, 04:08 PM

Tags for this Thread