reading file

This is a discussion on reading file within the C Programming forums, part of the General Programming Boards category; Hey guys im trying to read a file seperated by '|' with 8 fields and 7 '|' for some reason ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    reading file

    Hey guys im trying to read a file seperated by '|' with 8 fields and 7 '|'
    for some reason i cant successfully read a new field which i added menually to the file in the program. Iv tried copying it into the struct but then i get a pointer to integer error am i going about this the correct way?

    Code:
    struct node
    {
    
       int qtyInStock;
    }
    Code:
          /* Read the submenu file line by line, until there is no more to
             read */
          while(fgets(submenuLine, MAX_SUBMENU_LINE, submenuFileLoad) != NULL)
          {
             lineNumber++;
    
             submenuToken = strtok(submenuLine, DELIM);
             submenuNode = (ItemTypePtr)malloc(sizeof(ItemType));
    
             /* If cannot allocate memory, exit program */
             if(submenuNode == NULL)
             {
                fprintf(stderr, "\nMemory Allocation for item node failed!\n");
                fprintf(stderr, "Exiting program...\n");
                return EXIT_FAILURE;
             }
    
             numTokens = 0;
    
             while(submenuToken != NULL)
             {
                numTokens++;
    
                           
                
                /*loading qytInStock for each item*/
                if(numTokens == 8)
                {
                   
                   if((strlen(submenuToken) <0 ))
                   {
                     printf("Stock levels must be above 0\n");
                     printf("exiting on error\n");
                     free(submenuNode);
                     return EXIT_FAILURE;
                   }
                   
                    /*initializing the qtySold array to 0
                    for small medium and large*/
    
                    for(i = 0; i<NUM_PRICES; i++)
                    {
                    submenuNode->qtySold[SMALL] = 0;
                    submenuNode->qtySold[MEDIUM] = 0;
                    submenuNode->qtySold[LARGE] = 0;
                    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did you change your variable max_submenu_line to compensate for a longer line of data?

    Adak

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    oohhh yehhh i dont think i put enough

  4. #4
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    nope still getting the same problem

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by bazzano View Post
    nope still getting the same problem
    First, do you have the spacing right in the input line you now want? Newline at the end of the line, still?

    Second, print out what the tokens are right after you generate them. Is that the only problem, or is there a problem with later processing in the code?

    Third, are you getting any further warnings when you compile or link?

    After that, you'll have to post up some bit of code I can run for myself on your input line. I'd rather concentrate on why the tokens are failing from the modified input line.

    Adak
    Last edited by Adak; 05-08-2007 at 10:08 AM.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    As Adak said, it would be more better if your could show us a bit more of your code and a sample text while which u are trying to read. This would really help in find the error.

    And more over using strtok us not a good idea, cos it alters the system, if your need the originally string in the future u will have to back it up before u use strtok or u will have to use the alternative.

    ssharish20054

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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