Actually, I have a .csv file which has (userID,movieID,rating) seprated by commas. now, I have to read it and also make another .csv in the output. Can anyone tell me how to read a .csv file and take that data in integer (as userid , rating etc are integers). from google, i found that .csv is a normal plain text file that has data seprated by columns. Thanks in advance. :)
P.S help in C or either C++ will be helful. no problem with C++ as i have a good hand in C++ also. thanks.

Post a few lines of the csv (comma separated values):
http://en.wikipedia.org/wiki/Comma-separated_values

that you are having trouble with the input, and your code.

It's best to get into the specifics, and see the actual data and code, rather than work from generalities of either data or code.

It's fairly easy to accomplish in either language. In C, call fgets() to read each line, separate it into tokens with strtok(), then convert each string token into integers using atol() (or one of several other conversion functions).

In C++ it's a little simpler, call getline() with a line terminator (last parameter) as the comma, then use one of the string convesion functions to convert from string to int.

It's fairly easy to accomplish in either language.

True, provided the "CSV" file is actually a subset of CSV where each record is a single line and no quoting rules are in place. Properly parsing actual CSV files is not especially difficult, but it's not quite as easy as using fgets and strtok.

In general you are right, but I don't think his csv file contains any text -- all numeric. So complicating the code by parsing for quoted material is unnecessary.

@ancient dragon, yes my file is :

  • .csv file
  • contains all numeric data
  • that numeric data is seprated with commas

Can you explain getline function way a little bit more ?

@james sir, what are those "quoting" rules ?

thanks in advance to both of you.

#include<iostream>
#include<stdio.h>
#include <fstream>
#include <string>
using namespace std;

int main()
{
            ifstream myfile;
            myfile.open("hello.csv");
            string line="";
            char c= ',';

            if(myfile.is_open())
            {
                                while ( getline (myfile,line,c) )
                                {
                                         cout<<"userid: "<<atoi(line.c_str())<<endl;
                                         getline (myfile,line,c);
                                          cout<<"movieid: "<<atoi(line.c_str())<<endl;
                                         getline (myfile,line,c);
                                         cout<<"Rating: "<<atoi(line.c_str())<<endl;
                                         system("pause");
                                }

                                myfile.close();
            }
            system("pause");
           return 0;
}

but it is user-id . it read user id only first time. but then it is skipping the user-d and only giving movie-id and rating correctly. why is it so ? my triplet is like this :

(user-id, movie-id,rating)

but it is skipping user-id.what can be the possible reason ? thanks.

@james sir, what are those "quoting" rules ?

Read the links provided in this thread.

hmm... and what about my last post in which i posted one code ? Can you see that post also ? thanks in advance to you.

and what about my last post in which i posted one code ?

That's C++, not C. Can you post a sample of your file?

yes, here is the screen shot when i opened my file with notepad++. Click Here

4a85d4564766ef475dca28731ae19ec8

here is the screen shot when i opened my file with notepad++.

...was it so hard to copy and paste a few lines from the file into your post?

Anyway, I'd recommend reading the whole line first to avoid the issue you're seeing. getline doesn't recognize line breaks unless you tell it to do so. You're telling it to stop on a comma or EOF, which means the last field on the first line is "4.0\n1". Consider this:

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    ifstream in("test.txt");

    if (in) {
        string line;

        while (getline(in, line)) {
            stringstream sep(line);
            string field;

            getline(sep, field, ',');
            cout << "userid: " << field << '\n';

            getline(sep, field, ',');
            cout << "movieid: " << field << '\n';

            getline(sep, field, ',');
            cout << "rating: " << field << '\n';
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.