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
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.
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.
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.
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.
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.
Bookmarks