Read from a csv file

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

Read from a csv file

 
0
  #1
Oct 3rd, 2006
hi, i didn't get any reply probably coz ya didn't understand what i mean !
alright i'll make it simple.
i have a file in excel and i have to read from this file
here is the contents of this file
 <br />
[COLOR=seagreen]2,joined,2929828,429.6,2[/COLOR]<br />
[COLOR=seagreen]4,toytoy,2929299,122.8,19[/COLOR]<br />
[COLOR=seagreen]3,front shift,2299229,205,22[/COLOR]<br />
[COLOR=seagreen]3,shift,1111111,222,20[/COLOR]<br />
[COLOR=seagreen]...[/COLOR]<br />
[COLOR=seagreen]...[/COLOR]<br />
[COLOR=seagreen]...[/COLOR]<br />
I wrote this code
#include"stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
usingnamespace std;
typedefstruct sales 
{ 
int number;
char item[50]; 
char partnum[15];
float cost; 
char day[6];
};
sales rSales[197];
int _tmain(int argc, _TCHAR* argv[])
{
FILE *fp;
fp=fopen("cust.csv","r");
char bufferCustNum[40];
char bufferCost[40];
for (int i=0; i<197; i++)
{
fgets(bufferCustNum,3,fp);
rSales[i].number=atol(bufferCustNum);
fgets(rSales[i].item,sizeof(rSales[i].item),fp);
fgets(rSales[i].part_number,sizeof(rSales[i].partnum,fp);
fgets(bufferCost,6,fp);
rSales[i].cost=atof(bufferCost);
fgets(rSales[i].day,sizeof(rSales[i].day),fp);
}
for (int j=0; j<10; j++)
{
printf("number: %i\n", rSales[j].number);
printf("Item : %s\n",rSales[j].item);
printf("partnum: %s\n",rSales[j].partnum);
printf("bufferCost: %f\n",rSales[j].cost);
printf("day: %s\n\n",rSales[j].day);
}
system("pause");
return 0;
}
all what i'm trying to do is to store each part of the stream in the specific array and display it !
but the problem is that i'm still getting the ',' between the characters...
does any one know how to remove it without changing the struct ?!
or any idea how to write a while loop to read each character untill it reaches the comma and store it ?!
example: 2,joined,2929828,429.6,2
first it read the 2 and store it in rSales[i].number without storing the ',' and keep going untill it reach the end of the line !
i know how to write the pseudo but i dont know how to implement in the real code !
i'll appreciate any Help
thanks

DarkCoder+
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,639
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Read from a csv file

 
0
  #2
Oct 3rd, 2006
>> i didn't get any reply
I don't recall seeing another post -- did I miss it?

1. is that a C or a C++ program? You include both stdio.h and iostream, yet you use C file i/o functions. Make up your mind which kind of program you want and stick to it.

2. the code does not even compile cleanly. before attempting to answer your question you need to fix all the compile errors, then repost the code.

3. next time please use proper indentation -- the code is terrible to read as it is now. I hope you don't write all your programs like that.
Last edited by Ancient Dragon; Oct 3rd, 2006 at 7:39 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

Re: Read from a csv file

 
0
  #3
Oct 3rd, 2006
heheh
wel it was only a scratch i'm trying to do a totally different program...
and its C.
#include"stdafx.h"
#include<stdio.h>
#include<stdlib.h>
typedefstruct sales 
{ 
int customer_number;
char item[50]; 
char part_number[15];
float cost; 
char date[6];
};
sales rSales[197];
int _tmain(int argc, _TCHAR* argv[])
{
FILE *fp;
fp=fopen("sales.csv","r");
char bufferCustNum[40];
char bufferCost[40];
for (int i=0; i<197; i++)
{
fgets(bufferCustNum,3,fp);
rSales[i].customer_number=atol(bufferCustNum);
fgets(rSales[i].item,sizeof(rSales[i].item),fp);
fgets(rSales[i].part_number,sizeof(rSales[i].part_number),fp);
fgets(bufferCost,6,fp);
rSales[i].cost=atof(bufferCost);
fgets(rSales[i].date,sizeof(rSales[i].date),fp);
}
for (int j=0; j<10; j++)
{
printf("customer number : %i\n", rSales[j].customer_number);
printf("Item : %s\n",rSales[j].item);
printf("Part Number : %s\n",rSales[j].part_number);
printf("Cost: %i\n",rSales[j].cost);
printf("Date: %s\n\n",rSales[j].date);
}
return 0;
}
thx mate !
DarkCoder+
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,639
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Read from a csv file

 
0
  #4
Oct 3rd, 2006
you can use strtok() to extract the individual fields.
  1. while( fgets(buf,sizeof(buf),fp) != NULL)
  2. {
  3. rSales[i].number = atoi(strtok(buf,","));
  4. strcpy(rSales[i].item, strtok(NULL,","));
  5. strcpy(rSales[i].partnum, strtok(NULL,","));
  6. rSales[i].cost = (float)atof(strtok(NULL,","));
  7. strcpy(rSales[i].day, strtok(NULL,","));
  8. ++i;
  9.  
  10. }

[edit] you might want to read Dave's links from this post[/edit]
Last edited by Ancient Dragon; Oct 3rd, 2006 at 8:03 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

Re: Read from a csv file

 
0
  #5
Oct 3rd, 2006
aw thats beautifull !!!
cheers mate i'll work on strtok() !!! better know how it works
DarkCoder+
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 5166 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC