Read text file line by line and write lines to other files

This is a discussion on Read text file line by line and write lines to other files within the C Programming forums, part of the General Programming Boards category; Hello, How can read the file /var/etc/allInOne.cfg and split it into mltiple cfg files. the source file "/var/etc/allInOne.cfg "look like ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    6

    Read text file line by line and write lines to other files

    Hello,
    How can read the file /var/etc/allInOne.cfg and split it into mltiple cfg files.

    the source file "/var/etc/allInOne.cfg "look like this:

    line1
    line2
    ...
    line10
    filePath:/var/etc/file1.cfg
    line12
    line13
    ...
    line14
    filePath:/var/etc/file2.cfg
    linen
    ..
    filePath:/var/etc/filen.cfg

    Code:
    void genCfg(int err)
    {
    // create a buffer to read the allInOne.cfg.
    char buf[512];
    char linesbuf[2048];;
    // declare the variables we will use in this function
    // open temporary file cotaining the data 
    FILE *f = fopen("/var/etc/allInOne.cfg", "rt");
    if (f)
    {
     while (fgets(buf, 512, f)
     {
     // if we found this string the we can stop to read and create the cfg file then continue the read
     if (strstr(buf, "filePath:")) {
                                    FILE *fp;
                                    fp = fopen(buf,"w"); //the buf contain the path and filename
                                    /*Create a file and add text*/
                                    printf(fp,"%s",lines); /*writes data to the file*/
                                    fclose(fp); /*done!*/
                                    lines = ""; initialize lines for next cfg file
     }
     else 
      //store the line in the variable		       
      lines += buf;
    }
    // close file
    fclose(f);
    return 0
    }
    }
    Last edited by magische_vogel; 01-22-2011 at 11:59 AM.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Code:
    char lines;
    That's room for one character. You seem to need to study up on C strings, because that, along with
    Code:
    lines += buf
    is very wrong.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    6
    thanx for the reply,
    can you help me to make this function work properly please.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by magische_vogel View Post
    thanx for the reply,
    can you help me to make this function work properly please.
    Type "man string" at your terminal.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    6
    Quote Originally Posted by tabstop View Post
    Type "man string" at your terminal.
    i'm a newbie, can you explain how to read the path from the buf variable and pass it to the fopen function.

    best regards.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    strcpy will copy one string to another. You need to set a pointer to the beginning of the filepath, i.e., after the colon to mark the spot to start copying from.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    6
    Quote Originally Posted by tabstop View Post
    strcpy will copy one string to another. You need to set a pointer to the beginning of the filepath, i.e., after the colon to mark the spot to start copying from.
    Where is C or C++ Coders? can i see a snipet code on how to do that.I'm doing my best to solve it myself but i can't.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by magische_vogel View Post
    Where is C or C++ Coders? can i see a snipet code on how to do that.I'm doing my best to solve it myself but i can't.
    As a matter of fact, some of the very best are right here...

    You want to know how to copy a string to a string... look up strcpy() as suggested in your C library documentation.

    To locate the : and get your starting point you probably want something like strchr() which you should also look up in your docs...

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    6
    Quote Originally Posted by CommonTater View Post
    As a matter of fact, some of the very best are right here...

    You want to know how to copy a string to a string... look up strcpy() as suggested in your C library documentation.

    To locate the : and get your starting point you probably want something like strchr() which you should also look up in your docs...
    How to make this function work and create the target file :
    rename("/var/tmp/temp.cfg", line); //not working
    Code:
    FILE *fsp,*fdp;
               char *line = NULL;
               size_t len = 0;
               ssize_t read;
    
               fsp = fopen("/var/etc/allInOne.cfg ", "r");
               fdp = fopen("/var/tmp/temp.cfg", "w");
               if (fsp == NULL)
                   exit(EXIT_FAILURE);
               if (fdp == NULL)
                   exit(EXIT_FAILURE);
    
               while ((read = getline(&line, &len, fsp)) != -1) {
                      //printf("Retrieved line of length %zu :\n", read);
                      //printf("%s", line);
                   if (strstr(line, "/var/")) {
                      //Close the fdp file
                      fclose(fdp);
                      //get the path from line here
                      //how can i extract the path here and pass it to the rename function 
                      //Move the temp file 
                      rename("/var/tmp/temp.cfg", "/var/etc/file1.cfg"); //work good 
                      //rename("/var/tmp/temp.cfg", line); //not working
                      //Delete the temp file
                      remove("/var/tmp/temp.cfg"); 
                      //open the temp file                       
                      fdp = fopen("/var/tmp/temp.cfg", "w");
                      if (fdp == NULL)
                       exit(EXIT_FAILURE);
                   }
                   else {
                     fprintf(fdp,"%s",line); /*writes data to the file*/
                   }
               }
               //close the source file
               fclose(fsp);
               fclose(fdp);
               //free the pointer line
               free(line);

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    This is how you could do in awk.
    Code:
    /^filePath:/ {
         split($0,a,":");
         outfile = a[2]
         for(i=0;i < n;i++) {
           print lines[i] > outfile
         }
        close(outfile)
        n = 0
        next
     }
      { lines[n++] = $0; }
    For your c code, getline will also read '\n' in your buffer.

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    6
    Quote Originally Posted by Bayint Naung View Post
    This is how you could do in awk.
    Code:
    /^filePath:/ {
         split($0,a,":");
         outfile = a[2]
         for(i=0;i < n;i++) {
           print lines[i] > outfile
         }
        close(outfile)
        n = 0
        next
     }
      { lines[n++] = $0; }
    For your c code, getline will also read '\n' in your buffer.
    How to skip the '\n' in my buffer (line)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 11:04 AM
  2. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 09:59 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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