read from txt file C++


#1

I have a series of integers in a text file that I need to import and store in ‘int’ and ‘float’ variable. So far I have been able to import these values as std::string variables but need to convert them and don’t know how. if this is a lame question I am new to c++ and would appreciate any help that you can give me.

	// Declare the necessary variables.
	std::string line;
	// Define a file object.
	std::ifstream fileInput("C:/2dTrussMaterials.txt");

//	std::string material[3];
//	int matQuery = 0; 
//		// Read in all data.
//		while (std::getline(fileInput, line, '|')) { // Reads in a quote.
//			
//			// Print the quote.
//			std::cout << line << "
 - ";
//			
//			// Get and print the speaker.
//			std::getline(fileInput, material[matQuery]);
//			std::cout << line << "

";
//			
//			matQuery = matQuery+1;
//
//		} // End of WHILE.
//		
//		// Close the stream.
//		fileInput.close();	
//
//	//E modulus of elasticity
//	int e = material[0];
//
//	//A area of section
//	float a = material[1];	

#2

Hi.

Try using the iostream library. Here you have an example.


#3

So you just want to convert a string to an int? Can’t you just do atoi?


#4

I am able to import however, now i’m stuck with a string that i have to convert.

this is a sample of what I am doing. I would like to stick with this method for converting the string I know it can be done other ways but i would like to make this one work for other reasons that come into play later in the program. however, i may be open to other methods if this absolutly will not work.

for some reason when I convert the string into an int or flaot i gives me back a zero can some one tell me what is going on.

the text file that I am reading from has two numbers on two different likes. one is 20000 the other is 40.5

	// Declare the necessary variables.
	std::string line;
	// Define a file object.
	std::ifstream fileInput("C:/2dTrussMaterials.txt");

	//std::string s = 28383;
	//double x = convertToDouble(s);

	std::string material[3];

	int matQuery = 0; 
//		// Read in all data.
		while (std::getline(fileInput, line, '|')) { // Reads in a quote.
//			
//			// Print the quote.
//			std::cout << line << "
 - ";
//			
//			// Get and print the speaker.
			std::getline(fileInput, material[matQuery]);
//			std::cout << line << "

";
//			
			matQuery = matQuery+1;
//
		} // End of WHILE.
//		
//		// Close the stream.
		fileInput.close();	
//

	int e = atoi(material[0].c_str());
	float a = atoi(material[1].c_str());

#5

you should use stringstream instead, it provide the same conversion capability as iostreams.
Something like that :

#include <iostream>
int yourIntValue;
std:string yourStringFromGetline;

//while getline etc…
std::istringstream parser;
parser.str(yourStringFromGetline);
parser >> yourIntValue;
//end while


#6

ok this is what i’ve got now. it spits back different values than what I put into it.

they should be
e= 200000
and a = 4000

instead i get
e= -858993460
a = -858993472

this is the code i’m working with right now.

	// Declare the necessary variables.
	std::string line;
	// Define a file object.
	std::ifstream fileInput("C:/2dTrussMaterials.txt");

	//std::string s = 28383;
	//double x = convertToDouble(s);
	int txtmaterial[3];
	std::string material[3];

	int matQuery = 0; 
//		// Read in all data.
		while (std::getline(fileInput, line, '|')) { // Reads in a quote.
//			
//			// Print the quote.
//			std::cout << line << "
 - ";
			std::istringstream parser;
//			// Get and print the speaker.
			std::getline(fileInput, material[matQuery]);
//			std::cout << line << "

";
			
			parser.str(material[matQuery]);
			parser >> txtmaterial[matQuery];

			matQuery = matQuery+1;
//
		} // End of WHILE.
//		
//		// Close the stream.
		fileInput.close();	
//
	int e = txtmaterial[0];
	float a = txtmaterial[1];

I feel like it’s getting close to working you will have to excuse me if i am missing a basic step i’m just starting to learn c++ and am teaching myself. I appreciate all the help i can get thanks again


#7

could you start by using sinmple int and string before using arrays and pointers, that would probably make it easier to track the problem


#8

i have also been trying to do it this way.

convert.h

 // File: convert.h
 #include <iostream>
 #include <sstream>
 #include <string>
 #include <stdexcept>
 
 class BadConversion : public std::runtime_error {
 public:
   BadConversion(const std::string& s)
	 : std::runtime_error(s)
	 { }
 };

  inline int convertToInt(const std::string& s)
 {
   std::istringstream i(s);
   int x;
   if (!(i >> x))
	 throw BadConversion("convertToInt(\"" + s + "\")");
   return x;
 }

	inline int convertToFloat(const std::string& s)
 {
   std::istringstream i(s);
   float x;
   if (!(i >> x))
	 throw BadConversion("convertToFloat(\"" + s + "\")");
   return x;
 }

then i use those functions in the main cpp file

#include <stdio.h>
#include <cstdlib>
#include <math.h>
#include <fstream>
#include <string>
#include <iostream>
#include "convert.h"

// Declare the necessary variables.
	std::string line;
	// Define a file object.
	std::ifstream fileInput("C:/2dTrussMaterials.txt");

	//std::string s = 28383;
	//double x = convertToDouble(s);
	int txtmaterial[3];
	std::string material[3];

	int matQuery = 0; 
//		// Read in all data.
		while (std::getline(fileInput, line, '|')) { // Reads in a quote.
//			
//			// Print the quote.
//			std::cout << line << "
 - ";
			std::istringstream parser;
//			// Get and print the speaker.
			std::getline(fileInput, material[matQuery]);
//			std::cout << line << "

";

			parser.str(material[matQuery]);
			parser >> txtmaterial[matQuery];

			matQuery = matQuery+1;
//
		} // End of WHILE.
//		
//		// Close the stream.
		fileInput.close();	

//	//E modulus of elasticity

	int e = convertToInt(material[0]);
//
//	//A area of section

	float a = convertToFloat(material[1]); 

I’ve been at this for days and i’m getting soo confused some one please help me! lol thanks again.


#9

the method in the last post compiles fine with no errors however there seems to be a memory problem because when i run the plugin in maya it dosn’t give me any output. at all some error is breaking the commands i know the error is from this portion of code because without this code the plugin works fine.


#10

those are the part of the code i would look for error.

  • Could you test without using arrays ???
    once it will work fine , and when you need arrays in general you can use a std containers instead : vector .
    But i don’t see why you need an array of strings ???
    Just reuse the same
    std::string material;
    object for each
    std::getline(fileInput, material);

  • aint this loop blocking ?

  • oh and you are using getline twice !
    you should use “line” string variable (make sure it has the expected content by printing it) , convert it to stringstream and get the values from this.


#11

GOT IT! thank soo much every one.


#12

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.