| | |
dat,txt file and csv file????
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 55
Reputation:
Solved Threads: 0
Hi....... I have a complicated homework...... I have a data which is csv file. I have to take this data from csv file and do sum calculations on these data's elements. But my problem is I cant take data from csv file to do this...
I can take data from dat or txt file with
of course I have a structure on beginning my program and I know how to open file with fopen but it is not working when I try on csv file???
What is the difference why can not I do that????
note: when I take data from dat file there are only numbers or chars. but my csv file after every value there is a comma (,). Is this the problem???
PLease help.................
I can take data from dat or txt file with
C++ Syntax (Toggle Plain Text)
printf( "%s %s %d %f",abc.x,abc.y,abc.z,abc.w); fscanf("%s %s %d %f",abc.x,abc.y,&abc.z,&abc.w);
What is the difference why can not I do that????
note: when I take data from dat file there are only numbers or chars. but my csv file after every value there is a comma (,). Is this the problem???
PLease help.................
Teach me, Show me, Educate me :)
Are you attempting to write a C or C++ program? This is the C++ board. If you really mean a C program then I or one of the other mods will move it the the C board.
Excactly how to read the CSV file depends on what character is used as field separator. Normall is a comma, but can also be space or tab.
In a C program, I would use fgets() to get the entire line, then strtok() to split it up into its individual fields.
Excactly how to read the CSV file depends on what character is used as field separator. Normall is a comma, but can also be space or tab.
In a C program, I would use fgets() to get the entire line, then strtok() to split it up into its individual fields.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
if it is c++, using boost.tokenizer would be the easiest way.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <boost/tokenizer.hpp> #include <string> #include <vector> #include <iterator> #include <boost/lexical_cast.hpp> using namespace std; using namespace boost; struct abc { explicit abc( const string& line ) ; string x ; string y ; int z ; double w ; }; abc::abc( const string& line ) { typedef tokenizer< escaped_list_separator<char> > tokenizer ; tokenizer toker(line); tokenizer::iterator iterator = toker.begin() ; x = *iterator++ ; y = *iterator++ ; z = lexical_cast<int>( *iterator++ ) ; w = lexical_cast<double>( *iterator ) ; } int main() { const char* const file_name = "csv.txt" ; // assumes file has data for one abc per line: // string x, string y, integer z, double w (seperated by commas) // if x or y contain embedded commas, they would be quoted strings (' or ") ifstream file( file_name ) ; vector<abc> elements ; string line ; while( getline( file, line ) ) elements.push_back( abc(line) ) ; // will throw if there are errors }
Last edited by vijayan121; Aug 2nd, 2007 at 2:38 am.
![]() |
Similar Threads
- dat file txt file help???? (C)
- Txt File (Visual Basic 4 / 5 / 6)
- uploading .txt file via phpmyadmin to Mysql (MySQL)
Other Threads in the C++ Forum
- Previous Thread: help wanted asap
- Next Thread: Please helpwith how to return two dimensional array
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






