Not understanding reading from CSV

Hello everyone.

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';

the .csv file looks like this:

1,2,3,4
blue, green, red, orange

the output I get is exactly like that:

1,2,3,4
blue, green, red, orange

----------------------------------------------------------------------------

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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <fstream>
using namespace 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
Last edited on
Reading CSV data is a common question also. There are many ways to approach it, but for simple CSV files, C++ makes it easy:

Info on CSV structure
http://www.cplusplus.com/forum/general/13087/#msg63077

Example
http://www.cplusplus.com/forum/general/17771/#msg89751
(This same thread has a link to a csv iterator by moorecm.)

Hope this helps.
No unfortunately we havent gotten to looping (next chapter). We have learned a little about if / else statements which i understand the concept.

thanks for your first post i appreciate the help. =)
Your professor wants you to read a CSV file, with potential errors, without loops!?

Search the site for "input validation" and "verifying an input" and the like for more.

Good luck!
This is basically what he wants, i just found an example finally... Thank you for everyone's help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
        const char delimiter = ',';		// character which separates the values in each line
	const int LINE_LEN = 100;
	const int 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;
}
Last edited on
Topic archived. No new replies allowed.