Thread: Beginner in C - File I/O Question

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    5

    Post Beginner in C - File I/O Question

    Hi everyone,

    I am working on a c program for my intro to C class that requires me to copy 4 strings to a file and format it as a paragraph. I am struggling with writing it over however, and have been attempting to use fgets to no avail.

    Currently, I just have it set to write one of the strings for testing purposes. The code compiles, but when I open the newly created file I don't see that anything was copied over. Can someone help me please? I sincerely feel like I have no idea what I'm doing.

    This is my program so far:
    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
    
    
    FILE *fp_out;
    
    
    char *str1= "Four score and seven years ago our";
    char *str2= "fathers brought forth on this continent,";
    char *str3= "a new nation, conceived in Liberty and dedicated";
    char *str4= "to the proposition that all men are created equal.";
    
    
    fp_out = fopen ("my_file", "w");
    
    
    if (fp_out != NULL )
            {
            while (fgets (str1, sizeof(str1), fp_out) != NULL )
                    {
                    fputs (str1, fp_out);
                    }
            fclose(fp_out);
            }
    
    
    else
            printf("I couldn't open the file \"my_file\".\n");
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    your trying to get str one from your file using fgets. as the file is empty i suspect the while loop will never run as it fails on the first test therefore str1 is never written to the file and the file closes.

    what ide are you using? you may be able to set a break point on the while loop and step through the code to see what is happening and if the while loop is running or not.
    im a learner like yourself so don't quote me to strictly just my 2 pence worth.
    coop

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    i just ran your code with a breakpoint as described the while loop wasn't running due to the fgets returning null. hint.... why do you need the whille loop if your not reading anything in?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to copy (and hence write) strings to a file, why are you calling fgets? That reads from the file instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2019
    Posts
    5
    Boy do I feel stupid. Omitting the while loop entirely got it to write properly, thank you! In all the examples I had seen of others using fgets i had seen it placed in while loops, so i assumed it was needed here as well.

  6. #6
    Registered User
    Join Date
    Jun 2019
    Posts
    5
    Our professor said this could be accomplished using either fgets, fgetc or fprintf -- being that we wanted to move one line at a time I thought fgets would be the logical answer. How would you approach this?

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    well assuming your program is correct as in having the strings defined i would use the 1 that prints a string to file rather than reads a string from a file

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    using fgets to read from a file is sencible to use in a while loop as it reads untill the buffer is full or end of file. if its eof then all is call however suppose you have a small buffer due to limited memory or the text file to br read in has 2000 lines. you need to keep looping till you reach eof. hence why or otherwise the use of the while loop with fgets.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Question -- From Absolute Beginner's Guide to C
    By Dghelerter in forum C Programming
    Replies: 5
    Last Post: 12-26-2013, 01:30 PM
  2. Beginner question: moving file please help.
    By Tjm7777 in forum C Programming
    Replies: 3
    Last Post: 04-11-2009, 06:58 PM
  3. Beginner Question - Linking a data file
    By noiprocs2 in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2008, 08:58 AM
  4. Beginner Text File Input Question
    By Ruggles in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2006, 02:18 PM
  5. Replies: 2
    Last Post: 05-12-2003, 04:40 PM

Tags for this Thread