Thread: File I/O #1 trouble

  1. #1
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112

    File I/O #1 trouble

    look at my different code below

    Code:
    #include<stdio.h>
    
    int main()
    {
          FILE *fp;
          char s[80];
          int loop = 0;
          fp = fopen("Text.txt","w");
    
          printf("\nEnter a few lines of text:\n");
          
          while(strlen(fgets(s,79,stdin)) > 0)
               fputs(s,fp);
        
          fclose(fp);
    }
    it cant working, when used 'while' but if i dont use 'while' like this
    i mean not any string recorded on the Text.txt
    if didnt using while, (so i can only writing at least one sentence as its equal to buffering size)

    Code:
    #include<stdio.h>
    
    int main()
    {
          FILE *fp;
          char s[80];
          int loop = 0;
          fp = fopen("Text.txt","w");
    
          printf("\nEnter a few lines of text:\n");
          
          fgets(s,79,stdin);
          fputs(s,fp);
        
          fclose(fp);
    }
    the string i write on the .c is recorded in the Text.txt
    i wonder why?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It would have been useful if you have given some description of the data you are inputting to your program. Your problem is not a general one - it depends on what data you are entering.

    You are aware that fgets() leaves a trailing newline in the string? Consider what that means for strlen(fgets(...)) unless you force an end-of-file condition.

    If you are aborting the program (e.g. hitting CTRL-C), the fclose() statement is never executed. File I/O is typically buffered, so that means pending output will not be physically written to the file. If the total amount of data you have entered is "small" (the notion of small is OS dependent, but probably larger than you are typing in manually) it will appear that nothing is being written to the file, since all output to the file will be dropped.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    i see.
    it seems
    Code:
    while(strlen(fgets(s,79,stdin)) > 0)
    isnt needed though,
    pressing CTRL-C will closing the .txt file, and it recorded any input on the program.
    before i know this method, im hitting X button on the top right window corner
    ah problem solved. Thank you very much grumpy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble with file pointers...
    By quasigreat in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 08:12 AM
  2. Trouble with FILE & allegro
    By f0rg0tten in forum C Programming
    Replies: 2
    Last Post: 12-01-2005, 06:04 AM
  3. Having file i/o trouble
    By LiKWiD in forum C++ Programming
    Replies: 1
    Last Post: 07-28-2004, 05:20 AM
  4. Trouble with file io
    By will_mac in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2003, 02:55 AM
  5. trouble reading from a file...
    By Nutka in forum C Programming
    Replies: 7
    Last Post: 12-02-2002, 10:59 PM