How to Split a string into various types (integer, float, char)?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akdenizk
    New Member
    • Nov 2012
    • 1

    How to Split a string into various types (integer, float, char)?

    Hello there,

    I'm reading lines from a text file in C++ which contains integer + string + float number(like 3,67 with comma) + string in this order. I need the float number to sort the lines but I couldn't manage to separate the data into the types I can use so far. I tried different kind of functions and the best I could do was such a code;

    Code:
    void main (){
    	 ifstream records;
    	 records.open("records.txt");
    	 
    	int id;
    	string line;
    	char name[100];
    	float gpa;
    	string depart;
    	if (records.is_open()) {
    		while (!records.eof()) {
    			getline(records, line);
    			istringstream deneme(line);
    					deneme >> id;
    					deneme >> name;
    					deneme >> gpa;
    					deneme >> depart;
    
    	//		     cout << id << " -- " << name << " -- " <<  gpa1 <<  " -- " << depart << " -- " << endl;
    
    		
    		}
    	}
    	records.close();
    	system("PAUSE");
    }
    This fails at reading the floating number which has comma in it and then last string is read as string starting with the comma and rest of the number. An output example is:

    698 John 3 ,67

    It doesn't read last string on the line as well. I understand that part but simply I need another read but what I want exactly is to separate one line using "tab" as a seperator into proper data types and then using the numbers as integers, and the grades as floating numbers. How Can I do this?
    Last edited by Rabbit; Nov 28 '12, 05:19 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    c and c++ uses a dot to separate the whole number from the decimal part. Read it in as a string, replace the comma with a dot, and then convert it to a float.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Could the string in the middle column ever contain a space?

      Comment

      • solidpoint
        New Member
        • Nov 2012
        • 5

        #4
        To Rabbit's point, if the file is small, and you don't need an ongoing solution, just load the file into notepad and replace "," with "."


        You might want to try the Unix/Linux utility Awk. MS Access has a good import Wizzard that will make these problems very manageable, and you you'll have a trial & error environment to see if you guessed right. Once in Access you have lots of output options - like .csv

        Comment

        • divideby0
          New Member
          • May 2012
          • 131

          #5
          Not sure if it will help or not, but if you need to replace the ',' with a '.', then maybe

          Code:
          long pos;
          
          getline(records, line);
          
          if((pos = line.find(",")) != string::npos)
          {
              line.at(pos) = '.'; // replace the comma
              --pos; 
              while(isspace(line.at(pos))) 
              {
                  // remove any spaces if need be
                  line.erase(pos, 1);
                  --pos;
              }
          }
          
          isstringstream dename(line);
          ...
          just check the output before actually trying to use the string stream

          Comment

          • Rock R
            New Member
            • Jan 2012
            • 3

            #6
            Can you make a struct containing member variables that map the data types you read when you parse a line.eg (int, string, float).

            after you read a line (in getline function in your code) create a new struct of this type and implement conversion of data to member variables in its constructor(arg ument for constr will be string). Inside constructor you can use string tokenizer to parse string and assign appropriate values in member variables. I dont think this should be difficult. After creating the struct implement a predicate so you can sort the struct as per your need. Hope this helps.

            Comment

            Working...