![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2005
Posts: 3
Rep Power: 0
![]() |
Hi, guys. I'm looking for a little help. A little background on my problem: I am teaching myself some file manipulation, and trying to work with a csv file to seperate data.
I am going to post code that works, and describe what changes affect it negatively and how beneath the code. What I am asking for is the reason for the problems I am having with these changes so that I may learn. Thanks alot! #include <conio.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <fstream>
using namespace std;
void Finput();
void Foutput();
//-----------------Variable Declarations--------------------//
char d1n[9],d2n[9],d3n[9],d4n[9];
//-----------------Variable Declarations--------------------//
////////////////////////////////////////////////////////////////////////////////FINPUT BEGIN
void Finput()
{
//-----------------Variable Declarations--------------------//
ofstream fout;
string d1,d2,d3,d4;
//-----------------Variable Declarations--------------------//
cout<<"Data 1"<<endl;
cin>>d1;
cout<<"Data 2"<<endl;
cin>>d2;
cout<<"Data 3"<<endl;
cin>>d3;
cout<<"Data 4"<<endl;
cin>>d4;
fout.open("test.csv");
fout<<d1<<","<<d2<<","<<d3<<","<<d4<<","<<endl;
fout.close();
Foutput();
}
////////////////////////////////////////////////////////////////////////////////FINPUT END
////////////////////////////////////////////////////////////////////////////////FOUTPUT BEGIN
void Foutput()
{
//-----------------Variable Declarations--------------------//
ifstream fin;
int counter,cumcounter,newcounter,columncounter;
string alldata;
//-----------------Variable Declarations--------------------//
fin.open("test.csv");
fin>>alldata;
counter=0;
cumcounter=0;
columncounter=0;
while(counter<alldata.length())
{
if (alldata[counter]==',')
{
newcounter=0;
columncounter++;
for(int x=counter;x>cumcounter;cumcounter++)
{
switch(columncounter)
{
case 1: d1n[newcounter]=alldata[cumcounter];
break;
case 2: d2n[newcounter]=alldata[cumcounter];
break;
case 3: d3n[newcounter]=alldata[cumcounter];
break;
case 4: d4n[newcounter]=alldata[cumcounter];
break;
}
newcounter++;
}
cumcounter++;
counter++;
}
else
{
for (int y=0;y<1;y++)
counter++;
}
}
fin.close();
cout<<endl<<endl<<endl<<"d1n: "<<d1n<<endl<<"d2n: "<<d2n<<endl<<"d3n: "<<d3n<<endl<<"d4n: "<<d4n<<endl<<endl<<endl;
}
////////////////////////////////////////////////////////////////////////////////FOUTPUT END
main()
{
Finput();
system("PAUSE");
}
/Program Summary: Writes 4 inputs to a csv file and seperates them with commas. It then reads in the data from the file and sorts through it, seperating the entire stream back into seperate variables by recognizing commas. Problem 1: d1n,d2n,d3n, and d4n have to be global for the program to work. For a reason I cannot understand, if I declare them inside the Foutput function, I will get garbage characters mixed with the normal variables at the end of the program where it outputs the new, seperated data. Problem 2: If i set d1n,d2n,d3n,d4n as strings, like I would like to do (to avoid having to put a limit on the character array), the program will not output anything when i do a cout<<d1n<<d2n<<d3n<<d4n; . I'm not sure why this is. If I cout<<d1n[0], it will successfully output that single character, but another problem that comes with it, is that for some reason, d1n,d2n,d3n,and d4n all end up being equal to the same thing at the end of the program. I have checked to make sure my case is breaking as it should be, and can find no explanation for this. It seems that maybe strings cannot use the same assignment commands as character arrays can, but I thought they could? Also, putting in the data from the alldata string has no problem using the typical character array assignment code. Any help is very much appreciated. Thanks Much, Andrew Walker |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
What compiler are you using, in what OS?
Oh, and by the way, please tab your code. It makes it so much easier to read. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Aug 2005
Posts: 3
Rep Power: 0
![]() |
Windows XP OS, Compiler is the freeware Dev C++ from Bloodshed. Can you further explain where I should tab my code? By default, I tab any open brackets within another set of brackets. So any switch/if statement, loop, etc I tab over. Is there a more standard way of tabbing than this?
Thanks for your help. |
|
|
|
|
|
#4 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
1. The reason you get a different result when d1n, d2n etc are global vs local is that the global variables get set to contain all 0 bytes, whereas the local ones do not - you dont have any code to null terminate your strings, so if there isn't a zero byte already in the d1n, d2n arrays, then the program doesn't know when the strings stop. You can solve this by adding lines like:
switch(columncounter)
{
case 1: d1n[newcounter]='\0';
break;
case 2: d2n[newcounter]='\0';
break;
case 3: d3n[newcounter]='\0';
break;
case 4: d4n[newcounter]='\0';
break;
}2. Using strings. You should be able to declare d1n,d2n etc as strings and then use += to append to them: switch(columncounter)
{
case 1: d1n += alldata[cumcounter];
break;
case 2: d2n += alldata[cumcounter];
break;
case 3: d3n += alldata[cumcounter];
break;
case 4: d4n + alldata[cumcounter];
break;
}Note - you might be better off declaring an array for the d1n, d2n etc string dn[4]; dn[columncounter] += alldata[cumcounter]; |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Aug 2005
Posts: 3
Rep Power: 0
![]() |
Great, thanks for all your help. One question I have about your post though. When you say I might be better of declaring an array of strings like string dn[4], are you saying that that statement should create 4 different strings? I thought that syntax would simply create a string 4 characters long and that the only way to create an array of strings is to make a 2d array of a string, having to declare a fixed length of the string in the process. Let me know if this is incorrect.
Thanks again for the help, I learned alot. Andrew Walker |
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
If you're using the std::string type, the strings are variable-length. The Dark is correct.
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: May 2006
Posts: 1
Rep Power: 0
![]() |
A helping question
Hi
I am a newbie to the world of computing. I was searching for a post that will let me make a csv file from c++ prog. I saw your quwstion. It is written so neatly, that I actually understood that way to write csv files of my own!! Thanks anyway Gagan |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|