I have a csv file that is being read as a continous string of different characters and values(integers and Real #'s).

I separated these 35 different values by interating with factor of 35.

My goal now is to convert these different characters and values into int, float, and keep some as strings.

I have began trying to do some below, but I can only do it with integer values and it does not seem to work for float values. I also can't figure how to switch more than two ranges of values while converting them into arrays.

for (unsigned i = 0; i < all_words.size(); i+=35)
	{
		outfile << all_words[i] << "\t" << all_words[1+i] << "\t" << all_words[2+i] << "\t" << all_words[3+i] << "\t" << all_words[4+i] << "\t" << all_words[5+i] << "\t" << endl;
		
		string word = all_words[1+i];
		double RID;
		istringstream ss( word );
		ss >> RID;
		cout << RID << endl;

	
	}
}

what type is all_words? Maybe you can post a compilable example and demonstrate the problem with some sample input, the current output, and the expected output?

what type is all_words? Maybe you can post a compilable example and demonstrate the problem with some sample input, the current output, and the expected output?

#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
#include <locale>

using namespace std;

int main()
{
    ifstream infile("C:\\Dokumente und Einstellungen\\Yaser\\Eigene Dateien\\Internship\\C++_Code\\Sample1.txt");
	ofstream outfile("C:\\Dokumente und Einstellungen\\Yaser\\Eigene Dateien\\Internship\\C++_Code\\Output.txt");


	string line = "";
    vector<string> all_words;

    while (getline(infile, line))
	{
        stringstream strstr(line);
        string word = "";
		strstr.imbue(locale("german_germany.1252"));
        while (getline(strstr,word, ';')) 
		all_words.push_back(word);
    }
		
	for (unsigned i = 0; i < all_words.size(); i+=35)
	{
		outfile << all_words[i] << "\t" << all_words[1+i] << "\t" << all_words[2+i] << "\t" << all_words[3+i] << "\t" << all_words[4+i] << "\t" << all_words[5+i] << "\t" << endl;
		
		string word = all_words[i];
		int RID;
		istringstream ss( word );
		ss >> RID;
		cout << RID+RID << endl;

	
	}

	return 0;
}

Alright here it is. The input file and the output file are attached.

The outfile is just how I want to organize the file and values. I put the values into like columns and now I want to take each column from the outfile and convert it from a string to the format I want.

The first column was converted to int values as I have done successfully already. I add "RID" twice to make sure it was properly converted. I just can't figure how to convert the next column from the outfile into double and so forth.

I would also like to keep them as arrays during the process.

Thanks!

You can use the stringstream to convert the string to any valid numerical type, as long as the variable you are reading into is in scope. As long as you know the makeup of the file you can use control statements/loops to determine how many in each line to convert into ints vs doubles vs whatever and when to switch from one variable type to another. So, say the file is made up as follows: each line has 5 delimited fields---int, int, double, double, int. You have successfully extracted each field into a string and have stored each string in an array/vector called tokens. You now want to convert each string into it's numerical value:

int tempI;
double tempD;
int i = 0;
for( ; i < 2; ++i)
{
    istringstream iss(tokens[i]);
    ss >> tempI;
    cout << tempI << ' ';
}
for( ; i < 4; ++i)
{
   istringstream iss(tokens[i]);
   iss >> tempD;
   cout << tempD << ;
}
istringstream iss(tokens[i]);
iss >> tempI;
cout <<  tempI;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.