469,347 Members | 1,999 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,347 developers. It's quick & easy.

Reading an external csv file

Hi! I'm working on a piece of code for school. =) in C++. (I've read the rules and understand that i can't put all of the code here and ask for all of the code) I've got all (or most) of the code done.

I'm having trouble with the output.

I'm supposed to:
  1. Enter a csv file (with info in it)
  2. Read the file
  3. Go through it one by one and sort it into a structure
  4. Sort it by the customer id (which is also used in the csv file)
  5. Print it out in this format - cust_num;item;part_number;cost;date
  6. And put each of the sales (in that format) back into a .txt file

I've done all of this already. Its done so that the output prints out in the cmd window and into the txt file.

First Problem;;
This is the output in the cmd window:
0;tainless Steel Exhaust System;1006266;462.350006;29
1;eat Cover;1023589;15.250000;27
1;HC Valve;1056658;23.500000;1
1;uel Pump;1091662;56.180000;24
1;am Shaft;1037371;98.699997;1
As you can see -- its missing the first letter of the item name. But when it gets to customer number 10, the whole name is shown:
10;Rocker Cover Housing;1030241;42.169998;21
10;Hub Caps;1009393;50.029999;24
10;Stainless Steel Exhaust System;1068170;462.350006;27
10;Spanner Set;1056456;15.500000;7
I think it may be how I capture the customer number:
Expand|Select|Wrap|Line Numbers
  1. int GetCustomer(FILE *file, int *number)
  2. {
  3.     int i;
  4.     char buffer[80];
  6.     // read in customer number
  7.     for(i = 0; i<=2; i++)
  8.     {
  9.         buffer[i] = fgetc(file);
  10.     }
  12.     buffer[i] = '\0';
  14.     // Convert string to int
  15.     *number = atoi(buffer);
  17.     return 0;
  18. }
I asked my teacher and she suggested changing the:
Expand|Select|Wrap|Line Numbers
  1. buffer[i] = '\0';
  2. to
  3. buffer[i+1]='\0';
I tried it but it didn't work.

Second problem;;

This is what part of the output in the txt file looks like:
tainless Steel Exhaust System 面面面面面面面面面面1006266 面面面面面,C29 面面 eat Cover 面面面面面面面面面面面面面面面面面面面面1023589 面面面面面 tA27 面面 HC Valve 面面面面面面面面面面面面面面面面面面面面1056658 面面面面面 A1 面面面 uel Pump 面面面面面面面面面面面面面面面面面面面面1091662 面面面面面R`B24 面面 ynchomesh 面面面面面面面面面面面面面面面面面面面面1057073 面面面面面33{B24 面面 am Shaft 面面面面面面面面面面面面面面面面面面面面1037371 面面面面面ffB1 面面面 adiator Cap 面面面面面面面面面面面面面面面面面面面1036043 面面面面面瀦PA7 面面面 xle 面面面面面面面面面面面面面面面面面面面面面面面1089 737 面面面面面楊C6 面面面 xle 面面面面面面面面面面面面面面面面面面面面面面面1093 533 面面面面面楊C17 面面 iring harness 面面面面面面面面面面面面面面面面面面1047566 面面面面面 B28 面面 adiator Cap 面面面面面面面面面面面面面面面面面面面1021754 面面面面面瀦PA9 面面面 ear Mirror 面面面面面面面面面面面面面面面面面面面1050480 面面面面面 aB9 面面面
This is what it looks like when i get to the customer 10 sales:
Spanner Set 面面面面面面面面面面面面面面面面面面面1056456 面面面面面 xA7 面面面
I asked my teacher about it and she said that it may be how I've had the program to read in the csv file:
Expand|Select|Wrap|Line Numbers
  1.     //declares a pointer of the type file
  2.     FILE *file, *output; 
  4.     sales mysales[196];
  6.     //Opens Sales.csv for reading
  7.     file = fopen("sales.csv", "r");
  9.     // Check if file opened
  10.     if(file == NULL)
  11.     {
  12.        printf("can't open file");
  13.        return 0;
  14.     }
  16.     int i = 0, j = 0;
  18.     // while not end of file read and sort
  19.     while (!feof(file))
  20.     {    
  21.        GetCustomer(file, &mysales[i].cust_num);
  22.        get_field(file,',',mysales[i].item);
  23.        get_field(file,',',mysales[i].part_number);
  24.        get_field_float(file,',', &mysales[i].cost);
  25.        get_field(file,',',mysales[i].date);
  27.        i++;
  28.     }
She couldn't help me much more other than that (busy class).

Before I editted/updated the file, earlier when I was going through it, the output in the txt file was the same as the output (above) in the cmd window. But now its got all that character and weird formatting. (I unfortunately saved over it :S)

If someone could please help me get on the right track, or maybe point out where its wrong, i'd really appreciate it. [I hope there's enough information here to get you going. =)]
Oct 12 '07 #1
1 2073
Sorry to double post, but I just wanted to add that I was using Microsoft Visual C++ 2005 Express for this. I've also checking it out using Dev-C++. So it doesn't seem like its a complier thing.

Can anyone help or just look at the code up there please?
Oct 19 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Hans-Joachim Widmaier | last post: by
24 posts views Thread by Hendrik Schober | last post: by
50 posts views Thread by Michael Mair | last post: by
28 posts views Thread by Andrew Poulos | last post: by
13 posts views Thread by swetha | last post: by
6 posts views Thread by =?Utf-8?B?emFsZHk=?= | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.