Thread: Stupid Queston of the Day

  1. #1
    Help
    Guest

    Stupid Queston of the Day

    I need to read a file (CSV) that looks like:

    Code:
    1,"New York","Image.bmp","Image2.bmp",335,25,8
    2,"Chicago","Image.bmp","Image2.bmp",332,25,3
    I'm guessing its probably something simple, but the strings may be a bit problematic...

    Any suggestions you had to offer would be greatly appreciated.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Read a whole line into the string and then analyse the string by realising that the individual parts of the line are seperated by comma's.

  3. #3
    Help
    Guest
    Ok, I tried this but I'm getting a mess of compiler errors with the first being:

    Code:
    cc-1008 cc: ERROR File = importer.c, Line = 48
      A new-line character appears inside a string literal.
    
          sscanf(string,"%d,%[^,],%[^,],%[^,],%d,%d,%d,
                        ^

    Here is the code I'm using (long):

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    
      int num, lfl, lf, lc, cf, rc, rf, rfl;
      int hlfl, hlf, hlc, hcf, hrc, hrf, hrfl;
      int avg, avgr, avgl, hr, hrr, hrl, db, tp;
      int type, surf, foul, cap, wspd, wdir;
      int temp4, temp5, temp6, temp7, temp8, temp9, temp10;
      int rain4, rain5, rain6, rain7, rain8, rain9, rain10;
      char name[25], pic1[25], pic2[25];
      char string[255];
    
      FILE *infile;
    
    /* Open the CSV File for reading */
    
      infile = fopen("park.csv", "r");
      if (infile == 0)
      {
          exit(1);
      }
    
    /* Read in the strings */
    
      while (fgets(string, 255, infile) != NULL)
      {
        sscanf(string,"%d,%[^,],%[^,],%[^,],%d,%d,%d,
         %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,
         %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,
         %d,%d,%d,%d,%d,%d,%d,%d,%d",num,name,pic1,pic2,
         lfl,lf,lc,cf,rc,rf,rfl,hlfl,hlf,hlc,hcf,hrc,
         hrf,hrl,avg,avgl,avgr,hr,hrl,hrr,db,tp,type,
         surf,foul,cap,wspd,wdir,temp4,rain4,temp5,rain5,
         temp6,rain6,temp7,rain7,temp8,rain8,temp9,rain9,
         temp10,rain10);
    
    /* Test Printf's */
    
         printf("Number: %d\n",num);
         printf("Name: %s\n",name);
         printf("Average: %d\n",avg);
         printf("WindSpd: %d\n",wspd);
         printf("Temp 3: %d\n",temp7);
      }
    
    /* Close the CSV File */
    
      fclose(infile);
    
    
    }

    Any idea where I've messed things up?

  4. #4
    Help
    Guest
    Hmm... on a different system this compiles, but I run it and get bad data output, with a RTE coming up saying that there is a MSVCRT.DLL error.

    Any ideas?

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Code:
    int num, lfl, lf, lc, cf, rc, rf, rfl;
    int hlfl, hlf, hlc, hcf, hrc, hrf, hrfl;
    int avg, avgr, avgl, hr, hrr, hrl, db, tp;
    int type, surf, foul, cap, wspd, wdir;
    int temp4, temp5, temp6, temp7, temp8, temp9, temp10;
    int rain4, rain5, rain6, rain7, rain8, rain9, rain10;
    char name[25], pic1[25], pic2[25];
    char string[255];
    OMG! Read about arrays... There has to be another way to do this but now i gotta go. That just grabbed my attention!

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    string str1 = "this is a string literal with
                   newlines in it...";
    string str2 = "this string literal is broken up on "
                  "multiple lines without embedding unwanted "
                  "newlines or spaces";
    1,"New York","Image.bmp","Image2.bmp",335,25,8
    2,"Chicago","Image.bmp","Image2.bmp",332,25,3
    I count 1 integer, followed by 3 strings, followed by 3 integers - why do you have that big-a$$ sscanf() for one line?

    When scanf() style functions read an integer (%d), they expect to a pointer to store the result...
    Code:
    int n;
    scanf("%d",&n); //read an integer into n
    gg

  7. #7
    Help
    Guest
    Originally posted by Codeplug

    I count 1 integer, followed by 3 strings, followed by 3 integers - why do you have that big-a$$ sscanf() for one line?
    Because in reality that file I posted is actually 46 entries long (1 int; 3 strings; rest ints)... I just didn't want to type a sample data field that long.

    Thanks for the tip, I'll give it a try.

    Thanks for the help guys.

  8. #8
    Help
    Guest
    Originally posted by Codeplug
    [BWhen scanf() style functions read an integer (%d), they expect to a pointer to store the result...
    [/B]
    This was the issue, works like a charm now.

    Thanks a ton to all of you for your advice and patience.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Should I learn a FULL language first?
    By Raeliean in forum Game Programming
    Replies: 8
    Last Post: 07-16-2005, 06:59 PM
  2. stupid question of the day
    By davidnj732 in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2003, 02:46 AM
  3. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  4. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 11:50 PM
  5. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 03:50 PM