<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Help with reading a file in C, please.' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Help with reading a file in C, please.' posted on the 'Beginner C/C++' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Sun, 26 May 2013 01:41:54 -0700</pubDate>
    <lastBuildDate>Sun, 26 May 2013 01:41:54 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Help with reading a file in C, please.</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/423389/423389/help-with-reading-a-file-in-c-please/</link>
      <description>I have a file with 190 lines, that goes something like this:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;Antigua,English,Local dialects
Bahamas,English,Creole
Barbados,English
Belize,English,Spanish,Mayan,Carib
Canada,English,French
Costa Rica,Spanish,English
Cuba,Spanish&lt;/pre&gt;&lt;br /&gt;
First field is the country, following fields are the different languages spoken in that country.&lt;br /&gt;
Since some countries have more languages than others, I am stuck trying to read the file using fgets() and sscanf().&lt;br /&gt;
I'm doing this:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
while(i &amp;lt; SIZE &amp;amp;&amp;amp; 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++;
  }
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Any help please?&lt;br /&gt;
I have this big program to do, and can't do anything else until I am able to read the whole file.&lt;br /&gt;
&lt;br /&gt;
Thanks&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/beginnercpp/423389/423389/help-with-reading-a-file-in-c-please/</guid>
      <pubDate>Fri, 22 Apr 2011 21:56:57 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
    <item>
      <title>Re: Help with reading a file in C, please.</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/423389/423398/re-help-with-reading-a-file-in-c-please/#423398</link>
      <description>&lt;span style="color: Blue;"&gt;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.&lt;/span&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/beginnercpp/423389/423398/re-help-with-reading-a-file-in-c-please/#423398</guid>
      <pubDate>Sat, 23 Apr 2011 05:56:38 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
    <item>
      <title>Re: Help with reading a file in C, please.</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/423389/423402/re-help-with-reading-a-file-in-c-please/#423402</link>
      <description>Thanks a lot!&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/beginnercpp/423389/423402/re-help-with-reading-a-file-in-c-please/#423402</guid>
      <pubDate>Sat, 23 Apr 2011 09:55:10 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
    <item>
      <title>Re: Help with reading a file in C, please.</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/423389/423420/re-help-with-reading-a-file-in-c-please/#423420</link>
      <description>It's simple. Take a buffer say " char buf[] "&lt;br /&gt;
and use the function,&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;fread(buf,190,1,fp)&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
here in function "fread()"&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;buf&lt;/strong&gt; -is where the contents of the file will be stored.&lt;br /&gt;
&lt;strong&gt;190&lt;/strong&gt; -number of characters to read(190 in your case).&lt;br /&gt;
&lt;strong&gt;1 &lt;/strong&gt;  -number of items to read.&lt;br /&gt;
&lt;strong&gt;fp&lt;/strong&gt;  -file pointer pointing towards the file to be operated.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/beginnercpp/423389/423420/re-help-with-reading-a-file-in-c-please/#423420</guid>
      <pubDate>Sun, 24 Apr 2011 08:26:28 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
    <item>
      <title>Re: Help with reading a file in C, please.</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/423389/423446/re-help-with-reading-a-file-in-c-please/#423446</link>
      <description>&lt;span style="color: Blue;"&gt;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.&lt;/span&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/beginnercpp/423389/423446/re-help-with-reading-a-file-in-c-please/#423446</guid>
      <pubDate>Mon, 25 Apr 2011 04:16:08 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
  </channel>
</rss>