Checking for commas in imported files

This is a discussion on Checking for commas in imported files within the C Programming forums, part of the General Programming Boards category; I'm writing a program that imports files from a .csv. They problem is that every item in the file is ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    5

    Checking for commas in imported files

    I'm writing a program that imports files from a .csv. They problem is that every item in the file is separated by spaces instead of commas. I know I need to wrtie an if statement to check for commas, but how would I go about doing that and what would I do once that statement found a comma?

    Here's my code so far. This is just a subclass so some of the variables you see will not have initializations, but I assure you that they are there.

    I apologize in advance for the formatiing; code doesn't copy to messagebaords well.

    Code:
    void import(){
    FILE *in = NULL;
    char import[256];
    int z = 0;
    
        printf("Enter the number entries contained in the file: ");
        scanf("%d", &num);
    //getting the file name
         printf("Enter the name of the file you wish to import: ");
         scanf("%s", &import);
         in = fopen (import, "r");
    //adding the data to a link list
    	
         while (z < num) { 		
         ptr = (Node*)malloc(sizeof(Node)); 		
         fscanf(in, "%s %s\n", ptr->lname, ptr->fname); 	
         ptr->unique = z;
    //putting it all into one variable
         ptr->nextPtr = NULL; 		
         if (firstPtr == NULL) 
         firstPtr = ptr;
         else
         lastPtr->nextPtr = ptr;
         lastPtr = ptr;
    
         z = z + 1;
    	
    	}
    	fclose(in);
    printf("File imported successfully!\n");
    printf("%s", ptr->lname);
    printf("%s", ptr->fname);

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    There's a problem with your description...
    Quote Originally Posted by Ness757
    I'm writing a program that imports files from a .csv.
    By definition, .csv uses commas to separate values...

    Quote Originally Posted by Ness757
    They problem is that every item in the file is separated by spaces instead of commas.
    No problem. When you find a SPACE, following it is a new item.

    Quote Originally Posted by Ness757
    I know I need to wrtie an if statement to check for commas, but how would I go about doing that and what would I do once that statement found a comma?
    Not if the file is SPACE separated. You have to decide what a comma means in your file if SPACE is the delimiter.

    Quote Originally Posted by Ness757
    I apologize in advance for the formatiing; code doesn't copy to messagebaords well.
    Don't apologize if you know something is wrong. Fix it before posting. Spaces work best for indenting on forums, don't use TABs. There should be a setting in your editor to "convert TABs to SPACEs"
    Last edited by WaltP; 03-23-2006 at 08:32 AM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    Nevermind, guys, I figured out what the problem was and solved it. Thanks for contributing anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 06:55 AM
  2. Working with muliple source files
    By Swarvy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2008, 08:36 AM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  4. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21