0

I want to display csv data into rows and column form for example
name uid class
nnn 1 A
bbb 2 B
ccc 3 A

This is what I have tried so far:

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
    clrscr();
    ifstream ifile;
    char s[100], fname[20];
    cout<<"Enter file name to read and display its content" ;
    cin>>fname;
    ifile.open(fname);
    if(!ifile)
    {
        cout<<"Error in opening file..!!";
        getch();
        exit(0);
    }
    while(ifile.eof()==0)
    {
        ifile>>s;
        cout<<s<<" ";
    }
    cout<<"\n";
    ifile.close();
    getch();
}

this is my code for display csv data, but it actually shows all of the data on a single line.

Namita
  • 1
  • 1
  • What do you mean by display? Also, please show what you have tried. – Anon Mail Nov 14 '15 at 18:53
  • 2
    Possible duplicate of [How can I read and parse CSV files in C++?](http://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c) – 463035818_is_not_an_ai Nov 14 '15 at 18:57
  • tobi303 this page is not useful for me. i already check all the links related to this issue – Namita Nov 14 '15 at 19:00
  • @Namita Please add your code to the question where you can format it. As you can see, code in a comment is unreadable. – Anon Mail Nov 14 '15 at 19:03
  • when i enter filename.csv then it shows output [Number, Date, Marks, Name 1111,Nov 14,98,AW 2222,Nov 14,87,QD 3333,Nov 14,78,WD] continuously... how i get this data into rows and column form. please help .. and tell me what the code will be embedded for look like a structure of rows and column – Namita Nov 14 '15 at 19:11

3 Answers3

0

You can use the getline member function to grab a complete line. And then change this line:

cout<<s<<" ";

to

cout<<s<<"\n";

Also, I believe your while loop condition is incorrect.

Anon Mail
  • 4,660
  • 1
  • 18
  • 21
  • this code is execute properly but display data of csv file in continuous form ... – Namita Nov 14 '15 at 19:18
  • If you want to do further formatting then you need parse each line into fields, convert each field appropriately (e.g. is it an int, double, etc), then format the converted field appropriately. You can either do this in main or make a CSV reader class so the code can be reused. Check the link above about CSV readers. – Anon Mail Nov 14 '15 at 19:22
0

Use std::getline to read a line (including the commas). Make a std::stringstream from this line.

Then use std::getline to split the line according to the commas. You will have to use a nested loop.

Code sketch:

std::string line, word;

while (std::getline(ifile, line))
{
    std::stringstream stream(line);
    while (std::getline(stream, word, ','))
    {
        ... (whatever you want to do with each word)
    }
    std::cout << '\n'; // whatever you want to do at end of line
}
anatolyg
  • 26,506
  • 9
  • 60
  • 134
0

the cells in a row(line) of .csv file inspect with ';' sign.

for example when you read a line from .csv file, every column of that line, inspect with ';' sign and you can use of this for separate columns in a row!