Thread: Confusion how fgets() works

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    50

    Confusion how fgets() works

    My program counts the number of lines copied into a file, but when I decide to play around with fgets() I can't get it to do what I want it to do. I think my confusion is in the prototype itself and its parameters. For example, what if I just want to "load" a single line up to the new line character into my buffer array, then that leads me to think I change the !=NULL part in the while loop to !="0" null character where that's appended after the "\n" character.

    Another problem I don't understand is, I can max out my buffer array to size 1000,2000, 3000 whatever, it counts exactly the same number of lines each time. If I count by hand each character on the monitor per line the longest line character count including spaces is 21 plus 1 for new line character plus one for null character gives 23. I change buffer size to 23 it counts exactly the same number of lines which is great, but as I already said when it comes to "playing" with part of the file using fgets() it goes horribly wrong. Let's say I want to load a "word" into the buffer array alone when the first space is encountered, how do I interpret a space bar like the new line and null character?

    Code:
    #include <stdio.h>
    
    #define SIZE 2000
    
    int main(void)
    {
    
      FILE *input, *output;
      int count=0;
      char buffer[SIZE];
    
      if((input=fopen("C:\\file\\file.txt", "r"))==NULL)
      perror("File open failed");
    
      else
      {
           if((output=fopen("C:\\file\\file_new.txt", "w"))==NULL)
               perror("Output file open failed");
    
                else
               {
                   while((fgets(buffer, SIZE, input))!=NULL)
                   {
        
                        fputs(buffer, output);
                        count++;
                   }
              }
    
       printf("%d lines copied\n", count);
    
      }
    
      fclose(output);
      return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Jul 2014
    Posts
    2
    http://www.tutorialspoint.com/c_stan...tion_fgets.htm

    fgets is similar to gets function. It'll get all your string until meet '\n' or meet the max size.
    if you want space to be like new line, you have to code it after get string or use fread(),fscanf().

    You have to read the doc explaining about the function clearly what it exactly returns and what parameters do.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Tripswitch View Post
    how do I interpret a space bar like the new line and null character?
    you don't

    you read whole line using fgets and then parse the read string using something like sscanf, strchr, or strpbrk
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Your coding style is ugly. I cleaned it up. By the way, if you want to read word by word, may be better read char by char? What is your task exactly? Some word manipulation or what?

    Code:
    #include <stdio.h>
    
    
    #define SIZE 2000
    
    
    int main(void)
    {
        FILE* input;
        FILE* output;
        char buffer[SIZE];
     
        if( (input = fopen("C:\\file\\file.txt", "r"))
        && (output = fopen("C:\\file\\file_new.txt", "w")) ) {
            int count = 0;
            while( fgets(buffer, sizeof(buffer), input) ) {
                fputs(buffer, output);
                ++count;
            }
        }
    
    
        input? fclose(input) : perror("File open failed");
        output? fclose(output) : perror("Output file open failed");
        
        printf("%d lines copied\n", count);
        return 0;
    }
    Last edited by zub; 07-23-2014 at 10:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets() and strlen() confusion
    By rwagstaff in forum C Programming
    Replies: 11
    Last Post: 09-25-2012, 06:17 PM
  2. Use c++ to do my works on web
    By mozee in forum C++ Programming
    Replies: 7
    Last Post: 09-18-2010, 09:09 PM
  3. nothing works
    By psycho88 in forum C Programming
    Replies: 2
    Last Post: 12-01-2005, 11:23 AM
  4. Let's see how this works out
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 12-02-2001, 12:36 AM
  5. it never works...
    By Ryce in forum Game Programming
    Replies: 5
    Last Post: 08-30-2001, 07:31 PM