Thread: Something about fgets

  1. #1
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18

    Question Something about fgets

    I am writing a program in which the computer will read a txt file and encypt it. The encryption works fine, but the computer cannot read the file perfectly. If there's a newline, the scanning process stops. For example I have the following text in the txt file.

    One two three four five
    (newline) Six seven eight

    The computer will stop reading after 'five'. I assume that is because I use fgets. Can anyone help me? Is there any other function you can use to solve this?

    Thanks in advance.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Then you should open your file in binary mode "fopen(file, "rb");", and use "fread()" instead of other functions.
    Devoted my life to programming...

  3. #3
    Registered User Uli's Avatar
    Join Date
    Dec 2013
    Posts
    12
    I got it with while statement.
    It still working even if you use fgets function.
    But you have to open the file input and the file output together.
    Then you can declare fprintf in while statement.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Study this, and see if it helps. If I wanted to work with just text files, I would handle it in text or in binary, mode, depending on what else I wanted to do with them, and whether the program would ever be used for non-text files.

    Code:
    #include <stdio.h>
    
    #define MAX 100
    
    int main (void){
       char buff[MAX];
       char *c;
       FILE *fp=fopen("myFile.txt","r+");
       if(!fp) {
          printf("Error, file not found or did not open\n");
          return 1;
       }
       while((c=fgets(buff, MAX, fp))!= NULL) {
          printf("%s",buff);
       }
    
       fclose(fp);
       return 0;
    }
    Since fgets() includes the newline along with the row of text, the newline is no problem, at all.

    This is a little text file you can test the above program with:

    Near a lonely prison wall,
    I heard a young girl calling.
    "Michael, they have taken you away.
    For you stole Trevalyn's corn,
    so the young might see the morn.
    Now a prison ship lies waiting,
    in the bay."

    Low lie the fields of Athenry,
    where once we saw the small seabirds fly.
    Our love was on the wing, we had dreams,
    and songs to sing.
    Now it's lonely,
    'round the fields of Athenry.
    If your encryption program uses newlines in the body of the text, then you definitely need to use binary mode.

  5. #5
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18
    Oh, I see. Thanks everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with fgets
    By n3cr0_l0rd in forum C++ Programming
    Replies: 25
    Last Post: 06-10-2009, 08:36 AM
  2. fgets
    By mesmer in forum C Programming
    Replies: 1
    Last Post: 11-25-2008, 12:37 AM
  3. fgets
    By Axel in forum C Programming
    Replies: 16
    Last Post: 09-15-2006, 11:09 AM
  4. Using fgets?
    By cogeek in forum C Programming
    Replies: 3
    Last Post: 12-08-2004, 12:08 PM
  5. Fgets
    By MethodMan in forum C Programming
    Replies: 6
    Last Post: 02-26-2004, 07:05 PM

Tags for this Thread