•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 457,732 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,745 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 393 | Replies: 6
![]() |
•
•
Join Date: Oct 2008
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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!!
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
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
Knowledge is power -- But experience is everything
One of these, perhaps?
•
•
Join Date: Oct 2008
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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?
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?
•
•
Join Date: Jul 2008
Location: Bangalore, India
Posts: 104
Reputation:
Rep Power: 0
Solved Threads: 13
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
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
Last edited by ahamed101 : Oct 11th, 2008 at 2:18 pm. Reason: Corrected Post
regards,
Ahamed.
Ahamed.
•
•
Join Date: Oct 2008
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
c Syntax (Toggle Plain Text)
#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; }
Last edited by cscgal : Oct 13th, 2008 at 12:25 pm. Reason: Added code tags
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
Other Threads in the C Forum
- Previous Thread: how to reverse a numbers input by user
- Next Thread: Declaring Array



Linear Mode