简体   繁体   中英

Reading multiple data types from input file

I have 2 questions:

I am trying to take input from another file, initialize them into variables, then output the variables to another file. I am able to get all input into separate variables except for 1 line (the line should be a variable).

My input file contains this data:

557.9012043 0.673621489 7210984732 1.891092837
238 a 789.234 b
Yes Please
cannot wait for hawaii

The line "Yes Please" should be taken as a whole string, however, "Yes" and "Please" are getting separated.

My code for this is:

outFile << fixed << showpoint;
        outFile << setprecision(10);

        inFile >> w >> x >> y >> z >> int1 >> char1 >> float1 >> char2 >> string1
                >> word1 >> word2 >> word3 >> word4;

        outFile << w << endl;
        outFile << x << endl;
        outFile << y << endl;
        outFile << z << endl;
        outFile << float1 << endl;
        outFile << int1 << endl;
        outFile << char1 << endl;
        outFile << char2 << endl;
        outFile << string1 <<endl;
        outFile << word1 << endl;
        outFile << word2 << endl;
        outFile << word3 << endl;
        ouFile << word4 << endl;
}

When I run it, my outFile consists of:

557.9012043000
0.6736214890
7210984732.0000000000
1.8910928370
789.2340000000
238
a
b
Yes
Please
cannot
wait
for

How can I get the whole line, "Yes Please" assigned to my variable string1?

Question 2: My outFile has all of the extra 0's in the floating point variables because I set the precision. My goal, really, is to use the variables to solve equations. I would like the answers to the equations to only print 10 significant digits. How would I accomplish this task without the precision after the decimal being 10 digits?

I am very new to C++, so when if you answer, can you please explain why you gave the answer you did. I'm not trying to only receive answers, I want to learn. Thank you.

I solved the issue of getting a blank line after my getline() statement by adding myFile.ignore() before the getline() statement.

}
inFile >> w >> x >> y >> z >> int1 >> char1 >> float1 >> char2;
inFile.ignore();
getline(inFile, string1);
inFile >> word1 >> word2 >> word3 >> word4;
}

Now my string1 variable contains "Yes Please" as a whole string, they are no longer separated.

I recommend using a struct for each record. Also implement an overloaded operator>> to extract the record from an input stream.

struct Record_Floats
{
  double values[4];
  friend std::istream& operator>>(std::istream& inp, Record_Floats& rf);
};
std::istream& operator>>(std::istream& inp, Record_Floats& rf)
{
  inp >> rf.values[0] >> rf.values[1] >> rf.values[2] >> rf.values[3];
  return inp;
};

struct Record_Number_Letter
{
  double number1;
  char   letter1;
  double number2;
  char   letter2;
  friend std::istream& operator>>(std::istream& inp, Record_Number_Letter& rnl);
};
std::istream&  
operator>>(std::istream& inp, Record_Number_Letter& rnl)
{
  inp >> rnl.number1;
  inp >> rnl.letter1;
  inp >> rnl.number2;
  inp >> rnl.letter2;
  return inp;
}

By overloading the stream extration operator>> you can do things like:

Record_Floats record1;
my_data_file >> record1; // Read an all numbers record.
Record_Number_Letter record2;
my_data_file >> record2; // Read a record of number letter number letter.

Edit 1: Inputting words and lines
The input of a word is accomplished by:

std::string word_text;
my_data_file >> word_text;  

The input of a text line is accomplished by:

std::string text_line;
std::getline(my_data_file, text_line);

Answer 1: Eventually all the data in the infile and the outfile are big strings. whether it writes to a word or a string, separators(space, new line tab, comma etc.) remain the same. Obviously if YesPlease were written without a space all would have been well but it's not and C++ cannot differ between the cases of the 3rd and 4th lines. In order to solve this you must get the entire line of Yes Please into the string so you need to use std::getline.

Answer 2:I'll just give you a general algorithm...implementing is not unfamiliar.

1.construct a function which gets a string and returns a fixed string.
2a. turns string it into an array of chars.
2b.in the array it finds the location of the floating point.
3.if there's no floating point->number is an integer or a word->leave as is
4.otherwise if the last char is '0' and its index is bigger than index of floating 
point then change last char to '\0'(truncate last zero)
5.Repeat 4 until last char is not '0'.if last char is '.' then truncate it as well.
6.return the new string without the zeros

7.prepare outFile for reading and outFile2 for writing
8.get line from outFile, apply function to line and put the returned string  at the end of 
outFile2.
9.repeat 8 for all rows in outFile

outFile2 will look like outFile but with all the redundant zeros truncated.

edit: I got stumbled here with the mixed use of >> and std::getline for question 1. question 2 algorithm remains valid since it's supposed to use only getline.

I know it's sort of cheating but it gives the desired result if you use the following code:

outFile << fixed << showpoint;
    outFile << setprecision(10);

    inFile >> w >> x >> y >> z >> int1 >> char1 >> float1 >> char2 >> 
            >> word1 >> word2 >> word3 >> word4 >> word5 >> word6;

    outFile << w << endl;
    outFile << x << endl;
    outFile << y << endl;
    outFile << z << endl;
    outFile << float1 << endl;
    outFile << int1 << endl;
    outFile << char1 << endl;
    outFile << char2 << endl;
    outFile << word1;
    outFile << word2 << endl;
    outFile << word3 << endl;
    outFile << word4 << endl;
    outFile << word5 << endl;
    outFile << word6 << endl;
}

last edit:totally forgot about the use of ignore()...I see you already found the answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM