Thread: Writing to File?

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Question Writing to File?

    Someone please help me! As a project for my class I am supposed to write a program of my own, however, I have become stuck on something.

    When I open a text file like this:
    fopen(file_name, "w")

    How can I write to a specific line within the file; compared to appending it to the end of the file. Is there something that needs to replace "w"?

    This is the method that I use to write to the file now.
    fputs(the_record, out);
    fputc('\n', out);

    I am sure you can, but I am unsure of the placement and syntax of such code.

    Thanx in advance!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > How can I write to a specific line within the file;
    You can't - except for the special case of replacing an existing line with another line of the same length.

    If you want to insert lines into a file, you've got to read/write the whole file, and write out your new data at the appropriate moment.

    The only thing which doesn't require this is (as you've noted) is appending to the end of the file.

  3. #3
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43

    Cool

    What'd i'd do to write on say line 7 (while it may be messy) is just do this

    fprintf(out, "/n");
    seven times till whatever line number then print whatever you need after that. like i said it's only good if your going to do this everytime. but i'm just a novice, what'd I know? :-P

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    8
    My instructor said that you can search a file's lines for a "null", a line that is left blank within the file, and then write to that line. She didn't know the syntax for it, but she remembered doing something like that.

    Salem, how do you replace an existing line with another of the same length?

    The reason why I need to know how to do this is because I want to have a text file that already contains lines of text like so:

    Line 1
    Line 2

    Line 4
    Line 5

    I want to know the code which would enable me to write to Line 3, but not effect the other lines. If it is possible.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > My instructor said that you can search a file's lines for a "null",
    > a line that is left blank within the file, and then write to that
    > line. She didn't know the syntax for it, but she remembered
    > doing something like that.

    OMG. She didn't know the syntax for it? She can't find a blank line
    in a text file? My suggestion: get a new teacher.

    Besides, you can't do this anyway. An "empty line" is just a single
    character (or pair of, \n\r), not a huge blank spot in the file that you can insert text into...

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Lightbulb



    I actually figured it out on my own! Basically it is "Random Creation of a Record Text File". Quzah, your right. A blank line is nothing more than "\n\r". But in C you can access a file randomly, in this case records within a file, by using "fseek()" function. Using this formula, "begginning_of_record=(record_number - 1) * record_length" with the fseek() function I can locate a given record. I can also change the length of the record once located; otherwise, then write whatever I want to it.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > But in C you can access a file randomly, in this case records
    > within a file, by using "fseek()" function. Using this
    > formula, "begginning_of_record=(record_number - 1) *
    > record_length" with the fseek() function I can locate a given
    > record. I can also change the length of the record once located;
    > otherwise, then write whatever I want to it.

    Not in a text file. You're assuming fixed length records here. If you are scanning for lines of text, then you're not using records. Additionally, and this is the reason you can't do it accurately in text files, is the fact that some OS use '\n' while some use '\r', while some use '\n\r', to denote line ending.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The "begginning_of_record=(record_number - 1) * record_length" doesn't work because of the \n\r problem, but I think you can do this.

    Code:
    int lineno = 0;
    long offsets[100];
    long pos = ftell(fp);
    while ( fgets(buff,BUFSIZ,fp) != NULL ) {
        offsets[lineno++] = pos;
        pos = ftell(fp);
    }
    This creates an index of all the file offsets corresponding to where each line starts.

    To overwrite a line, you would do something like
    Code:
    fseek( fp, offsets[i], SEEK_SET );
    fputs( buff, fp );

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    With the /n/r thing could you do a function to find the OS then add in a switch statement using /n /r and /n/r respectivly for the OS?
    Also the comment about the teacher...his is pretty good my one friend took a course in C++(High school) graduated from it with a B. After wards we were talking I asked her what compiler she used and she looked at me like I had 3 eyes. I had to explane a compiler to one of the best students in the class. I'm scared what the dumb kids learned :-).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM