473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Reading an external csv file

8
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;ynchomesh;1057073;62.799999;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];
  5.  
  6.     // read in customer number
  7.     for(i = 0; i<=2; i++)
  8.     {
  9.         buffer[i] = fgetc(file);
  10.     }
  11.  
  12.     buffer[i] = '\0';
  13.  
  14.     // Convert string to int
  15.     *number = atoi(buffer);
  16.  
  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 ÌÌÌÌÌÌÌÌÌÌffÅB1 ÌÌÌÌÌÌ 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; 
  3.  
  4.     sales mysales[196];
  5.  
  6.     //Opens Sales.csv for reading
  7.     file = fopen("sales.csv", "r");
  8.  
  9.     // Check if file opened
  10.     if(file == NULL)
  11.     {
  12.        printf("can't open file");
  13.        return 0;
  14.     }
  15.  
  16.     int i = 0, j = 0;
  17.  
  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);
  26.  
  27.        i++;
  28.     }
  29.  
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 2305
anii
8
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

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

Similar topics

0
by: Hans-Joachim Widmaier | last post by:
Hi, I have a class that reads most common files (text, images, tar archives, zip archives). I just read in the whole thing and decide then what to do with it - create a gtk.TextView, gdk.pixbuf,...
24
by: Hendrik Schober | last post by:
Hi, I have a 'std::istream' and need to read its whole contents into a string. How can I do this? TIA; Schobi
2
by: Hugh Macdonald | last post by:
I'm writing a tool at the moment that reads in an external file (which can use any Python syntax) At the moment, I'm reading the file in using: scriptLines = open(baseRippleScript).read()...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
24
by: ej1002 | last post by:
Hi I have developed a Windows Application(C# Windows Form) which will get the IFrame Source of the page it is navigating using Webbrowser Control. Now I want do this in ASP.Net web application(C#...
28
by: Andrew Poulos | last post by:
When loading an rss feed into Windows IE, doc.childNodes.length always equals 0. If I manually delete the <!DOCTYPE tag doc.childNodes.length is correct. I'm using doc = new...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
6
by: =?Utf-8?B?emFsZHk=?= | last post by:
Hi! I have a question.. How can csharp read a text file and execute it in another application? What I mean is that, Im doing a drawing using Tekla Structures. But instead of doing the same...
6
by: Charles Cain | last post by:
How can i ensure that i have received a file completely from an external source before reading it?Suppose i am receiving a file from an external source and i try to read the file before i have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.