2 Replies - 3494 Views - Last Post: 18 January 2011 - 06:12 PM Rate Topic: -----

#1 peter1971  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-January 11

I want to read data from text file delimited by comma and find small n

Posted 18 January 2011 - 01:52 AM

I am a beginner in C++ programming and i want to create a program that will read data from text file delimited by comma, the result is stored in vector <string> record, and i want to convert the data contents of record because i want to determine the largest number..but it does not works..i dont know what is the problem...please help me..

the data
test.txt
12,45,60,90
13,70,1,2

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include<vector>
#include <stdlib>
using namespace std;
int main(){

  vector <string> record;
  
  ifstream infile( "test.txt" );

  while (infile)
  {
    string s;
    if (!getline( infile, s )) break;

    istringstream ss( s );
    

    while (ss)
    {
      string s;
     
      if (!getline( ss, s, ',' )) break;
      record.push_back( s );
    }
    int number_string = record.size();
    int convertInt[number_string];  
    for(int i=0;i<number_string;i++){    
    
   cout<<record[i]<<" ";
        }
     
     for(int a=0;a<number_string;a++){
             convertInt[a] = atoi(record[a]); //error std::allocator<char> >' to `const char*' for  
                                             //   argument `1' to `int atoi(const char*)' 
             }

  }


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 18 January 2011 - 04:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: I want to read data from text file delimited by comma and find small n

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: I want to read data from text file delimited by comma and find small n

Posted 18 January 2011 - 05:03 AM

First it's #include <cstdlib>, not #include <stdlib>. Second, record[a] is C++ string, and atoi expects a C-string. You can get the C-string from record[a] with record[a].c_str(). However, the better way to do this would be to use a stringstream:

stringstream convStrm;
for(int a=0;a<number_string;a++){
    convStrm << record[a]; // Insert the string to the stringstream
    convStrm >> convertInt[a]; // Extract the string from the stringstream to an integer
    convStrm.str(""); // Reset the string stream
}

Was This Post Helpful? 2
  • +
  • -

#3 peter1971  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-January 11

Re: I want to read data from text file delimited by comma and find small n

Posted 18 January 2011 - 06:12 PM

View PostJackOfAllTrades, on 18 January 2011 - 05:03 AM, said:

First it's #include <cstdlib>, not #include <stdlib>. Second, record[a] is C++ string, and atoi expects a C-string. You can get the C-string from record[a] with record[a].c_str(). However, the better way to do this would be to use a stringstream:

stringstream convStrm;
for(int a=0;a<number_string;a++){
    convStrm << record[a]; // Insert the string to the stringstream
    convStrm >> convertInt[a]; // Extract the string from the stringstream to an integer
    convStrm.str(""); // Reset the string stream
}



thanks JackOfAllTrades, but when i tried to display the contents of convertInt[a], the output are 16384 0 4200062 not 12 45 60 90 13 70 1 2 the original contents of record[a] below is the code:
stringstream convStrm;
for(int a=0;a<number_string;a++){

convStrm << record[a]; // Insert the string to the stringstream
convStrm >> convertInt[a]; // Extract the string from the stringstream to an integer
cout<<convertInt[a];//display the integer contents of convertInt
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1