User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,609 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,625 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 160 | Replies: 2
Reply
Join Date: Jul 2008
Posts: 1
Reputation: dannomite is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dannomite dannomite is offline Offline
Newbie Poster

stingstream and assigning fields of csv files to variables

  #1  
Jul 8th, 2008
Hello all.

I'm trying to read a line of data in from a csv file and then assign the fields to it to an int w, string y, and double z **See code below**


However, I have two problems:

1. The first is in reading in the all contents of the second field and assigning it to a string y WITH white-space included. If I compile this code it will only output the first word in the second field even with the noskipws **line 36**

2. The second is reading in the complete number with both decimal places (only the 12 shows) and assigning it to a double z **lin 42**

For simplicity sake the contents of file "clients.txt" read are:

503,long meadow,12.29

But the output to screen is:

503 long 12


  1. #include <cstdlib>
  2. #include <fstream>
  3. #include <ios>
  4. #include <iomanip>
  5. #include <iostream>
  6. #include <sstream>
  7. #include <string>
  8. #include <sstream>
  9.  
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14.  
  15. int w;
  16. string word;
  17. string y;
  18. double z;
  19.  
  20. ifstream infile("clients.txt");
  21.  
  22. while (getline( infile, word ))
  23. {
  24. if (word.empty()) continue;
  25.  
  26. istringstream ss( word );
  27.  
  28. { string val;
  29. getline( ss, val, ',' );
  30. stringstream( val ) >> w;
  31. cout<<" "<<w<<" ";
  32. }
  33.  
  34. { string val;
  35. getline(ss,val, ',' );
  36. stringstream( val )>>noskipws>> y;
  37. cout<<y<<" ";
  38. }
  39.  
  40. { string val;
  41. getline( ss, val, ',' );
  42. stringstream( val ) >> z;
  43. cout<<z<<endl;
  44. }
  45.  
  46. }
  47.  
  48. system("PAUSE");
  49.  
  50. return 0;
  51.  
  52. }
Last edited by Ancient Dragon : Jul 8th, 2008 at 3:15 pm. Reason: add line numbers
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,546
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: stingstream and assigning fields of csv files to variables

  #2  
Jul 8th, 2008
1) line 36: you don't need that line because line 35 has has already assigned val all the words including spaces .

You can use stringstream to convert from std::string to int or float, but I find it more convenient (in most cases) to just use atol() and atof(). There are problems with using those two functions, but if you KNOW the file contains no errors and the numbers are in normal range then there should be no problems using them. If there are some problems with the values in the file then you should use stringstream so that you can capture errors better.
#include<iostream>
#include <sstream>
using namespace std;

int main()
{
    std::string word = "503,long meadow,12.29";
    int w;
    float z;
    string val;

    stringstream ss(word);
    getline(ss,word,',');
    w = atol(word.c_str());
    cout << w << "\n";
    getline(ss,word,',');
    cout << word << "\n";
    getline(ss,word,',');
    z = (float)atof(word.c_str());
    cout << z << "\n";
}
Last edited by Ancient Dragon : Jul 8th, 2008 at 3:26 pm.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,546
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: stingstream and assigning fields of csv files to variables

  #3  
Jul 8th, 2008
lines 28, 34 and 40. Don't declare val in each of those blocks. Just declare it once at the begining of the function and reuse it everywhere in your program. Its also not necessary to block out those like you did on lines 28 and 32.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:09 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC