-
December 20th, 2010, 05:30 PM
#1
Not sure how parse this file correctly
I am very new when it comes to C and C++ as well as the fact that I haven't touched the language in over a year so I am having some trouble trying to figure out the syntax/logic for this particular problem I am having. Here is the fragment of code I am having trouble with:
1 #include <stdio.h>
2 #include <string>
3 int main (void)
4 {
5 FILE *inputFile;
6
7 char fe[6]="1.csv";
8 char reader[1];
9
10 if ((inputFile = fopen(fe, "r")) == NULL)//r reads from the file pointer
11 {
12 printf("Error opening input file\n ");
13 exit(1);
14 }
15
16 for (int i = 0; (i <= 247); i++)
17 {
18
19 while(reader[i](inputFile)!=','||'\0'){
20 fscanf(inputFile,"%s",&reader[i]);
21 }
22 }
23
24}
What I am trying to do is parse through a CSV file and each set of data is seperated by a comma so I figured I could make a while look that will keep inputting the data into the string until it reaches a comma then increment "i" once more. This particular piece of code is just to skip through the file. I am sure this is an easier way to do this but I don't know it. The only real problem is line 19 I don't know how to write it so that the while loop will be looking at the file output.
Thanks for the help
-
December 20th, 2010, 05:45 PM
#2
Re: Not sure how parse this file correctly
Originally Posted by LorenzoVonMatterhorn
I am very new when it comes to C and C++ as well as the fact that I haven't touched the language in over a year so I am having some trouble trying to figure out the syntax/logic for this particular problem I am having. Here is the fragment of code I am having trouble with:
1 #include <stdio.h>
2 #include <string>
3 int main (void)
4 {
5 FILE *inputFile;
6
7 char fe[6]="1.csv";
8 char reader[1];
9
10 if ((inputFile = fopen(fe, "r")) == NULL)//r reads from the file pointer
11 {
12 printf("Error opening input file\n ");
13 exit(1);
14 }
15
16 for (int i = 0; (i <= 247); i++)
17 {
18
19 while(reader[i](inputFile)!=','||'\0'){
20 fscanf(inputFile,"%s",&reader[i]);
21 }
22 }
23
24}
What I am trying to do is parse through a CSV file and each set of data is seperated by a comma so I figured I could make a while look that will keep inputting the data into the string until it reaches a comma then increment "i" once more. This particular piece of code is just to skip through the file. I am sure this is an easier way to do this but I don't know it. The only real problem is line 19 I don't know how to write it so that the while loop will be looking at the file output.
Thanks for the help
Perhaps read each line using "fgets" in a while loop, and then interpret it accordingly. If a line has a ",", then break, otherwise do whatever with the line string.
Code:
while( fgets(line_buffer, buffer_size, fp ) )
{
if( std::find( line_buffer, line_buffer + strlen(line_buffer), ',' ) == strlen(line_buffer) ) break;
// handle the line_buffer
}
Note that
1. the buffer_size should be one greater that the max length required, i.e., to account for the null character.
2. fgets also receives the new line character.
-
December 20th, 2010, 05:45 PM
#3
Re: Not sure how parse this file correctly
The "reader" array only contains a single char. Your for loop lets i reach as high as 247. This is going to be a problem....
-
December 20th, 2010, 05:52 PM
#4
Re: Not sure how parse this file correctly
The original "reader" array is actually char reader[2000]; I accidentally changed it when I was rewriting that to be a much smaller bit of code
-
December 20th, 2010, 07:48 PM
#5
Re: Not sure how parse this file correctly
Originally Posted by LorenzoVonMatterhorn
The original "reader" array is actually char reader[2000]; I accidentally changed it when I was rewriting that to be a much smaller bit of code
Don't rewrite anything into this forum. Rewrite it into a small example project and then compile it. Many times you can resolve problems during that process. If you can't then you can post it within code tags (use the # button above the edit box) and a question.
-
December 20th, 2010, 07:51 PM
#6
Re: Not sure how parse this file correctly
I recommend searching the forums for a solution. I'll bet that you will find many questions about processing CSV files. Your initial post doesn't lead me to believe that you put much effort into this on your own.
-
December 20th, 2010, 08:04 PM
#7
Re: Not sure how parse this file correctly
I did compile it several times and it did not help at all, however other than line 19 of the code everything else was functioning, I will take some time later tonight to sift through the other threads thank you kempofighter. I have been looking for several days however I have not taken the time to search through this particular particular forum.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|