CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Dec 2010
    Posts
    3

    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

  2. #2
    Join Date
    Feb 2008
    Posts
    22

    Re: Not sure how parse this file correctly

    Quote Originally Posted by LorenzoVonMatterhorn View Post
    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.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

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

  4. #4
    Join Date
    Dec 2010
    Posts
    3

    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

  5. #5
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Not sure how parse this file correctly

    Quote Originally Posted by LorenzoVonMatterhorn View Post
    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.

  6. #6
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    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.

  7. #7
    Join Date
    Dec 2010
    Posts
    3

    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
  •  





Click Here to Expand Forum to Full Width

Featured