Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help with reading a file in C, please. Posted by veyer on 22 Apr 2011 at 9:56 PM
I have a file with 190 lines, that goes something like this:

Antigua,English,Local dialects
Bahamas,English,Creole
Barbados,English
Belize,English,Spanish,Mayan,Carib
Canada,English,French
Costa Rica,Spanish,English
Cuba,Spanish

First field is the country, following fields are the different languages spoken in that country.
Since some countries have more languages than others, I am stuck trying to read the file using fgets() and sscanf().
I'm doing this:
while(i < SIZE && fgets(buffer, 255, fp))
  {
    sscanf(buffer, "%[^,]%*c%[^\n]", holdName, buffer);//read the country first, and keep the rest for the languages 
    .........
    
    /* Here is where I'm lost, I want to read a language, keep the rest of the buffer to repeat until
        the buffer is empty. But obviously I'm doing it wrong */
    while(sscanf(buffer, "%[^,]%*c%[^\n]", holdLang, buffer) != '\n')
    {
      ............
    }
    i++;
  }


Any help please?
I have this big program to do, and can't do anything else until I am able to read the whole file.

Thanks
Report
Re: Help with reading a file in C, please. Posted by AsmGuru62 on 23 Apr 2011 at 5:56 AM
Better way would be to use fgets() to read one line of the file and then use strtok() to parse that line using comma separator.
Report
Re: Help with reading a file in C, please. Posted by veyer on 23 Apr 2011 at 9:55 AM
Thanks a lot!
Report
Re: Help with reading a file in C, please. Posted by MrNinux on 24 Apr 2011 at 8:26 AM
It's simple. Take a buffer say " char buf[] "
and use the function,

fread(buf,190,1,fp)

here in function "fread()"

buf -is where the contents of the file will be stored.
190 -number of characters to read(190 in your case).
1 -number of items to read.
fp -file pointer pointing towards the file to be operated.
Report
Re: Help with reading a file in C, please. Posted by AsmGuru62 on 25 Apr 2011 at 4:16 AM
This code will not work. 190 is approximate # of lines in file - not characters per line. And lines will not be the same length, so reading constantly by 190 characters will skip some short lines and lines will be loaded not from the beginning of each line, but from some place in file which is divisible by 190 bytes.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.