I'm a C novice, require help on parsing text files and storing in an array or another text file:

Below is the data which are partly pipe delimited values and part of it are name-value pairs,
for instance the "cma{PL}Ind{1}man{6}gal{0}cif{}" mentioned below
cma is the field name and PL is its value, i need to extract only the values within the braces along with other pipe delimited values. Please suggest.

The data looks like this:

1|2|3|?|feg|jfh||||"cma{PL}Ind{1}man{6}gal{0}cif{}"|
111|233|333|?|media|jack|||||"cma{PPS}Ind{1}man{3}gal{0}cif{234}ink{0}"|

Need to store the values in a array.


Please help!!

strtok() would help. read some online documentation on it.

It splits a string based on a delimiantor
such as a pipe, this would allow you to retrieve all the different ssctions between the pipes. Then you could further process the {} out of the data.

Chris

That program gave a good idea.. But suppose i need to write the contents of the text file into another text file after the file parsing, do i need to store it in a array or can do itthe read and write operation directly.

For ex:
Input file
1|2|3|?|feg|jfh||||"cma{PL}Ind{1}man{6}gal{0}cif{}"|

Output file:
1|2|3|?|feg|jfh||||PL|1|6|0||

that is as i mentioned earlier i need the values withhin the double quotes parsed only with values

Could you suggest a code for this please?

You can read and write almost directly :)

You can read and write simultaneously like Sci@phy suggested...

1. Open file 1 in read mode
2. Open file 2 in append mode
3. read from file 1
4. do the processing
5. write to file 2
6. continue from step 3 till end of file of file 1
7. close file 2
8. close file 1

Thanks for the suggestion, I'm having trouble while writing to another file.
Suppose i have 2 lines in the input file,the output file has one line. I know its the newline character problem,But not sure how to solve it. One more problem is since i have 2 delimiters( | and {}) within the same line, how do i actually append it to the same line after the processing.

#include <stdio.h>
#include <string.h>

int main(void)
{
   static const char filename[] = "file.txt"; /* the name of a file to open */
   FILE * pFile;
   FILE *file = fopen(filename, "r"); /* try to open the file */
   pFile = fopen ("myfile.txt","a");
   if ( file != NULL )
   {
      char line[BUFSIZ]; /* space to read a line into */
     
      char data[1][32];
      while ( fgets(line, sizeof line, file) != NULL ) /* read each line */
      {
         size_t i = 0, size;
         char *token = line;
         fputs(line, stdout);
         for ( ;; )
          {
            size_t len = strcspn(token, ";\n"); /* search for delimiters */
            sprintf(data[i], "%.*s", (int)len, token);
            fprintf (pFile, "%s|",data[0]);

            token += len; /* advance pointer by the length of the found text */
            if ( *token == '\0' )
            {
               break; /* advanced to the terminating null character */
            }
            ++token; /* skip the delimiter */
          }

       }

      fclose(file);
      fclose(pFile);
   }
   else
   {
      perror(filename); /* why didn't the file open? */
   }
   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.