Writing to a text file

This is a discussion on Writing to a text file within the C Programming forums, part of the General Programming Boards category; I've been away from programming for a while, and fear that I may have over-indulged in some form of brain ...

  1. #1
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140

    Post Writing to a text file

    I've been away from programming for a while, and fear that I may have over-indulged in some form of brain killing activity...If one of you good people would be kind to point out what I'm doing wrong with this (simple) program, I'd be most gratefull...

    Code:
    #include <stdio.h>
    
    
    #define STR_SIZE sizeof(struct TEST)
    #define C_SCANF while(getchar() != '\n');
    
    struct TEST{
       char msg[100];
       int  number[2];
       }msg[10];
    
    
    int main(void)
    {
    
    int count = 0;
    
    FILE *fp;
    
    
    if ( (fp=fopen("test.TXT","rt+"))== NULL)
         {
          if ( (fp=fopen("test.TXT","wt"))== NULL)
             {
              printf("\nUnable to create test file");
              return 1;
             } 
          }
    
    
    while(count < 10)
      {
       printf("\nEnter a message > ");
       fgets(msg[count].msg,800,stdin);
    
       printf("\nEnter a number (I don't care) > ");
       scanf("%d", msg[count].number);
       C_SCANF;
    
       fseek (fp,(count)*STR_SIZE, SEEK_SET);
       fwrite(&msg,STR_SIZE,1,fp);
    
       count++;
       }
    }
    Thanks for any help..
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  2. #2
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    Well, yes... that helps.... I'm off to cower in a corner, I'm becoming worse at this...
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  3. #3
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    Oooh I have a related question...

    Am I correct in assuming that fseek can only be used with binary files?
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Inept Pig
    Oooh I have a related question...

    Am I correct in assuming that fseek can only be used with binary files?
    no.
    hello, internet!

  5. #5
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    A most insightful answer, Thanks....
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > Am I correct in assuming that fseek can only be used with binary files?
    Yes and no

    You can't use fseek() in the way you have used it - namely calculating the position.

    You can however use ftell() to tell you where you are, then use that result in future fseek() calls.

    Incomplete example to read a random line from a file.
    Code:
    unsigned long positions[10];
    int i = 0;
    
    // produce an index of the file
    positions[i++] = ftell(fp);
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
        positions[i++] = ftell(fp);
    }
    
    // use the index to select a random line in a file
    int j = rand() % i;
    fseek( fp, positions[j], SEEK_SET );
    fgets( buff, BUFSIZ, fp );
    printf( "Random line from file %s", buff );
    This technique is pretty useful if your text file is very long, and fairly constant (say a dictionary). You can jump to the start of a line without having to read each line in turn from the start of the file.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    Splendid!

    Thanks for answering in such detail.

    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21