Thread: Not reading a file correctly?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    Not reading a file correctly?

    Code:
    fscanf(fp,"%d",&num_names);
    while(fgets(temp,80,fp)!=NULL)
    {
    char *ptr;
    ptr=strtok(temp,",  .");
    
    
    while(ptr!=NULL)
    {
    tokens[i]=ptr;
    ptr=strtok(NULL,",  .");
    i++;}
    }
    i-=1;
    printf("%s",tokens[]);
    return 0;
    }
    
    
    
    
    names.txt:
    6
    Ryan, Elizabeth O.
    McIntyre, O. J.
    Cauble-Chantrenne, Kristin K.
    Larson, Lois F.
    Thorpe, Trinity R.
    Ruiz, Pedro M.

    What wrong?

    I think there is an error with my delimiters. ", ."
    When i printf tokens[0], tokens[1],token[2] i get ruiz, pedro ruiz
    tokens[3]= blank
    tokens[4]=O
    tokens[15]= ends program
    Last edited by tmac619619; 11-13-2012 at 01:49 AM.

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Unless fgets will scan the whole file up to 80 chars??

    whoaaa..


    or maybe i have to use sscanf?
    Last edited by tmac619619; 11-13-2012 at 02:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program is not file-scanning correctly
    By C0__0D in forum C Programming
    Replies: 3
    Last Post: 11-01-2012, 10:07 PM
  2. Am I reading User input into Arrays correctly?
    By threwup in forum C Programming
    Replies: 9
    Last Post: 02-10-2011, 07:49 PM
  3. Having issues with indexing correctly in a file
    By McFury in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2008, 10:55 AM
  4. file not opening correctly
    By scwizzo in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2007, 01:23 PM
  5. File I/O not working correctly
    By gL_nEwB in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2006, 10:29 PM