Thread: Lopping through flat files

  1. #1
    guestquestion
    Guest

    Lopping through flat files

    i'm a pretty novice C programmer. What i am trying to do is, take the following flat file. What i want to do is output each word in the flat file using a "," as a seperator.

    Example:
    this , is , an , example

    What i want to do is output in some form of a loop:
    (like in php using the foreach() function)

    and output each word outputting:
    first element: this
    second element: is
    third element: an
    fourth element: example

    I hope this doesn't sound jibberish. I'm trying to explain as best as i can. Thanks in advance for anybody whom can help me.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Code:
    #include <stdio.h>
    #define MAXLEN 200
    
    int main()
    {
         FILE *in, *out;
         char InLine[MAXLEN], OutLine[MAXLEN];
         char *s1, *s2;
    
         if(in = fopen("infile.txt", "r")) 
         {
              if(out = fopen("outfile.txt", "w"))
              {
                   while(fgets(InLine, MAXLEN, in))
                   {
                        s1 = InLine; s2 = OutLine;
                        while(*s1)
                        {
                             *(s2++) = *(s1++);
                             if(*(s1-1) == ' ')
                             {
                                  *(s2++) = ',';
                                  *(s2++) = ' ';
                             }
                        }
                        *s2 = 0;
                        fputs(OutLine, out);
                   }
                   fclose(out);
              }
              fclose(in);
         }
         return 0;
    }
    Last edited by Scarlet7; 06-05-2003 at 03:44 PM.

  3. #3
    guestquestion
    Guest
    Salem--i know how to utilise file handling

    And Scarlet, thanks for taking the time out to code that, but your incremenation for the "," seems alot more work than i thought. I was looking for a function that i could use to seperate each member of the file by seperator. You get me?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Maybe something like this? Here are the contents of my "file.txt":
    Code:
    this , is , an , example
    this,is,another,example
    and , this,is, also an, example ,
    this isn't, (just kidding!)
    The code:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if(file != NULL)
       {
          char buffer[BUFSIZ], fmt[32];
          int i = 0;
          sprintf(fmt, "%%%lu[^,]%%*c", (long unsigned)sizeof(buffer));
          while(fscanf(file, fmt, buffer) == 1)
          {
             printf("%3d \"%s\"\n", i++, buffer);
          }
          fclose(file); /* [edit]I missed this, CodeMaster gets the credit[/edit] */
       }
       return 0;
    }
    My output:
    Code:
      0 "this "
      1 " is "
      2 " an "
      3 " example
    this"
      4 "is"
      5 "another"
      6 "example
    and "
      7 " this"
      8 "is"
      9 " also an"
     10 " example "
     11 "
    this isn't"
     12 " (just kidding!)
    "
    Note the newlines that remain in the output because the comma is the only delimiter.
    Last edited by Dave_Sinkula; 06-08-2003 at 05:13 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    guestquestion
    Guest
    Excellent, thanks

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    14
    Dave_Sinkula,

    I love your code resolution.

    However, you did not utilize fclose for the file pointer
    to FILE.

    Maybe I missed something, could you correct me if
    I did.

    Just helping out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM