dat,txt file and csv file????

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 55
Reputation: jerryseinfeld is an unknown quantity at this point 
Solved Threads: 0
jerryseinfeld jerryseinfeld is offline Offline
Junior Poster in Training

dat,txt file and csv file????

 
0
  #1
Aug 1st, 2007
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
  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.................
Teach me, Show me, Educate me :)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,337
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1459
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #2
Aug 1st, 2007
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

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

 
0
  #3
Aug 2nd, 2007
if it is c++, using boost.tokenizer would be the easiest way.
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC