Thread: printing to a file

  1. #1
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123

    printing to a file

    I need help using fprintf (if indeed that is what I should be using)

    char word[1000][80];
    FILE *output;
    output=fopen("file1.txt", "w");
    for (i=0;i<no_of_words;i++) {
    fprintf(output, "%s", word[i]);
    fprintf(output, word[i]);
    }
    fclose(output);

    This creates the file and modifies it (txt editor assures me) but the file contains nothing... nada... zip...

    I included both my attempts at implementing fprintf but neither produce any visible output in "file1.txt". Hope I've included enough code.

    Any help would be great, thanks...

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    ... I don't want to be a smartass, but perhaps this isn't working because of what you're sending to the file... I'd suggest trying this...
    Code:
    for (i = 1000; i != 0; i--) char[i] = "schnagop!";
    Anyhow, do whatever it takes to make sure the character arrays aren't all '\0' or something like that.

  3. #3
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123
    Okay... whoops...

    It wasn't _that_ simple but I had indeed just done a stupid mistake.

    Next question

    I have an input file containing 28 words (one per line). We are supposed to assume that the file is too big to fit into memory and only read in half the file at a time. I can now perfectly read in half the file, sort it, and write it to a tempory file.

    I now have to read in the second half of the file... is there a way to tell fgets to start at a certain point or do I have to do something ugly like read the first half again (into, say, char temp[] each time) then start reading into the array when (line in file = start of second half) using an 'if'?

    Any solution out there better than my hideous one?

    Cheers...

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Use ftell to set the position and fseek to go back to that position in the file.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Well... there's fseek and ftell... but after you finish reading the first half of the file... won't the FILE * still be at halfway through the file, so you can just keep using it? As long as you don't close it, everything should be cool.

  6. #6
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123
    Thanks, I'll try both of these ideas...

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    One thing I would add just to be on the safe side is a check to make sure the file was opened.

    Code:
    if ( (output=fopen("file1.txt", "w")) == NULL)
         {
         printf("Can not open file1.txt");
         exit(0);
         }
    I know that it doesn't really change the operation of the program but having that check has helped me more then once.

    Thantos

  8. #8
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123
    Yeah, that test's been there since the beginning... program's working fine now.... cheers...

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > but after you finish reading the first half of the file... won't the FILE * still be at halfway through the file, so you can just keep using it? As long as you don't close it, everything should be cool.
    As you say, if you don't close the file, then you can pick up where you left off some time ago.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    It is not a good idea to leave the data file open.
    This could destroy the data if an error occurs, program closes.
    Functions should open, read/write and close files in as little operatons as possible,unless you have a VERY good reason to leave them open.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > This could destroy the data if an error occurs, program closes.
    It's a good idea for writing, but reading should be OK

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM