944,113 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3872
  • C++ RSS
Aug 1st, 2007
0

dat,txt file and csv file????

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  1. printf( "%s %s %d %f",abc.x,abc.y,abc.z,abc.w);
  2. fscanf("%s %s %d %f",abc.x,abc.y,&abc.z,&abc.w);
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.................
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
jerryseinfeld is offline Offline
55 posts
since Apr 2007
Aug 1st, 2007
0

Re: dat,txt file and csv file????

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Aug 2nd, 2007
0

Re: dat,txt file and csv file????

if it is c++, using boost.tokenizer would be the easiest way.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <boost/tokenizer.hpp>
  4. #include <string>
  5. #include <vector>
  6. #include <iterator>
  7. #include <boost/lexical_cast.hpp>
  8. using namespace std;
  9. using namespace boost;
  10.  
  11. struct abc
  12. {
  13. explicit abc( const string& line ) ;
  14. string x ;
  15. string y ;
  16. int z ;
  17. double w ;
  18. };
  19.  
  20. abc::abc( const string& line )
  21. {
  22. typedef tokenizer< escaped_list_separator<char> > tokenizer ;
  23. tokenizer toker(line);
  24. tokenizer::iterator iterator = toker.begin() ;
  25. x = *iterator++ ;
  26. y = *iterator++ ;
  27. z = lexical_cast<int>( *iterator++ ) ;
  28. w = lexical_cast<double>( *iterator ) ;
  29. }
  30.  
  31. int main()
  32. {
  33. const char* const file_name = "csv.txt" ;
  34. // assumes file has data for one abc per line:
  35. // string x, string y, integer z, double w (seperated by commas)
  36. // if x or y contain embedded commas, they would be quoted strings (' or ")
  37. ifstream file( file_name ) ;
  38. vector<abc> elements ;
  39. string line ;
  40. while( getline( file, line ) )
  41. elements.push_back( abc(line) ) ; // will throw if there are errors
  42. }
Last edited by vijayan121; Aug 2nd, 2007 at 2:38 am.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help wanted asap
Next Thread in C++ Forum Timeline: Please helpwith how to return two dimensional array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC