Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,395 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,124 people online right now. Registration is fast and FREE... Join Now!




reading using fgets

 
Reply to this topicStart new topic

reading using fgets

beginnerInC
29 Sep, 2006 - 06:44 PM
Post #1

New D.I.C Head
*

Joined: 28 Sep, 2006
Posts: 8


My Contributions
i am trying to read a file using fgets.
the point is that i want to read a line in a csv file than splitting the columns of this file into arrays.
the code i am using is the following
CODE

#include <stdio.h>
#include <string.h>

#define max_r 762
#define max_c 7
  

typedef struct{
    int day;
    char month[3];
    int year;
    }DATE;

typedef struct {
    char StockName[20];
    DATE date;
    float ClosingPrice;
    }BHP;    
    
    


int main(void)
{

    FILE* infile;
    int i,j;
   /* BHP stock[max_r][max_c];*/
    char buffer[762];
    char delims[] = ",";
    char *result = NULL;
    
    infile=fopen("bhp.csv","r");
    if (infile == NULL)
        printf("Error opening the file!!\n!");
    /* Read the file*/
    for(i=0;i<762;++i)
       for(j=0;j<7;++j)
    fgets(buffer,max_r,infile);
    puts(buffer);
    /* spliting the columns*/
   result = strtok( buffer, delims );
   while( result != NULL ) {
       printf( "result is \"%s\"\n", result );
       result = strtok( NULL, delims );
   }  

}

my problem is that i am only reading the first line of the file
but i want to read all of it.
can anyone help?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Reading Using Fgets
29 Sep, 2006 - 08:25 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 45 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
You will need to use the feof function as the test condition of a while loop.

An example of its use can be found here.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:52AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month