I have an assignment where I must read data from a .csv and output a .txt as a "payroll" report.
The problem I am having is that I don't really understand how to get each seperated value as its own variable so that I can properly format the report at the end of the program.
When I try to inFile >> name; it grabs the entire row.
How do I set up my variables so that each csv is its own variable and I can easily format my table at the end (which I understand how to do).
I tried a "test" to make sure I was able to at least open the file and read from it, but like I said it grabbed the entire row for example my code looks like this:
char name[50]
inFile >> name;
cout << name << '\n';
inFile >> name;
cout << name << '\n';
I kind of understand why its taking the whole line with the commas, but at the same time I dont understand how to seperate each value into its own variable.
It's really tough because I'm basically trying to learn on my own and from the book because my professor isn't so great. The book only shows me how to read from a txt file which is easily understood.
I should mention we are very early off in the class and haven't gotten into anything very complex yet, I think the point is for us to use char arrays and learn how to format using setw().
Please any help will be sooooo greatly appreciated.
Thank you guys/gals for taking the time to read this over.
have you learned loop(for,while) and control(if,switch) commands? if u have, check this out:
(I named my input file "input.txt" you can name it "asdf.csv" or however u want it)
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
int numbers[4]; //an array to hold the integers
char colors[4][10]; //an array to hold 4 strings each one having 10 characters
ifstream fin;
fin.open("input.txt");
int i;
for (i=0; i<4; i++)
{
fin >> numbers[i];
fin.get(); //get the ',' characters and the newline character when i==3
}
char ch;
int char_count;
for (i=0; i<4; i++)
{
char_count=0;//for each string we start with char_count set to zero
while(true)
{
fin.get(ch);
if (ch!=',' && ch!='\n')
{
colors[i][char_count]=ch; //if ch isn't ',' or '\n' add it to the current string
char_count++; //move on to the next char of the current string
}
else
{
colors[i][char_count]='\0'; //a string must be zero-terminated
if (ch!='\n') fin.get(); //get the space character after ','
break; //move on to the next string
}
}
}
fin.close();
cout << "integers:\n";
for (i=0; i<4; i++)
{
cout << numbers[i] << endl;
}
cout << "strings:\n";
for (i=0; i<4; i++)
{
cout << colors[i] << endl;
}
cout << "hit enter to quit..." << endl;
cin.get();
return 0;
}
if u haven't progressed that far, tell me and I'll see how it can be made simpler
constchar delimiter = ','; // character which separates the values in each line
constint LINE_LEN = 100;
constint SIZE = 20;
ifstream inFile;
char month[SIZE];
/* It is assumed that "demofile.csv" and the program
reside in the same directory. */
inFile.open("demofile.csv");
/* Ignore "LINE_LEN" characters or until the new line character is encountered.
This advances the file pointer to the second line of the file.
Note: The value of "LINE_LEN" should be large enough so that all the characters
up to the new line could be ignored.
Note: If the new line character is encountered, it will also be ignored. */
inFile.ignore(LINE_LEN,'\n');
/* Store up to "SIZE - 1" characters in "month" or until the delimiter is encountered.
Note: If the delimiter is found, it is discarded. It is not stored in "month". */
inFile.getline(month,SIZE,delimiter);
cout << month << "\n"; // January
system("pause");
inFile.getline(month,SIZE,delimiter);
cout << month << "\n"; // February
system("pause");
/* Note: The new line character is used as the delimiter, because this is
the last value in the line. */
inFile.getline(month,SIZE,'\n');
cout << month << "\n"; // March
system("pause");
inFile.getline(month,SIZE,delimiter);
cout << month << "\n"; // April
system("pause");
inFile.getline(month,SIZE,delimiter);
cout << month << "\n"; // May
system("pause");
/* Note: The new line character is used as the delimiter, because this is
the last value in the line. */
inFile.getline(month,SIZE,'\n');
cout << month << "\n"; // June
system("pause");
/* Close the file */
inFile.close();
return EXIT_SUCCESS;
}