c++ parsing input string(with special symbols) to vector variables - c++

I want to do this:
I have created
vector<string> inputStates;
vector<string> finalStates;
I want to get the values of inputStates and finalStates by parsing the following two lines. They are read from a .txt file using getline(). How should I write the codes? Thank you.
Initial State: 1
Final States: {3,14,20,29,30}
The result should be
inputStates={"1"};
finalStates = {"3","14","20","29","30"}.
I think I should get element and then use finalStates.push_back() to append. But I really don't know how to get the elements from the strings.

You need to include an "fstream" to read the data from the text file and then create an input stream (ifstream)
#include <fstream>
ifstream myFile;
and then use the myFile stream to open your text file and read from it.
More information can be found here.
http://www.cplusplus.com/doc/tutorial/files/
When you read the string using the ifstream you can read it one word at a time and then push the appropriate numbers into your vector.

Related

Ignoring remaining newlines and white space when reading input file (C++)

I have a function that reads a text file as input and stores the data in a vector.
It works, as long as the text file doesn't contain any extra new lines or white space.
Here is the code I currently have:
std::ifstream dataStream;
dataStream.open(inputFileName, std::ios_base::in);
std::string pushThis;
while(dataStream >> pushThis){
dataVector.push_back(pushThis);
}
For example:
safe mace
bait mate
The above works as an input text file.
This does not work:
safe mace
bait mate
Is there any way to stop the stream once you reach the final character in the file, while still maintaining separation via white space between words in order to add them to something like a vector, stack, whatever?
i.e. a vector would contain ['safe', 'mace', 'bait', 'mate']
Answer:
The problem came from having two streams, one using !dataStream.eof() and the other using dataStream >> pushThis.
Fixed so that both use dataStream >> pushThis.
For future reference for myself and others who may find this:
Don't use eof() unless you want to grab the ending bit(s) of a file (whitespace inclusive).

C++ Reading from a file given by user [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm creating a simple directory program that allow user to enter a file name and search that file by name, address, or phone number. I'm having trouble properly reading the file.
If someone could give me suggestions on how to fix my getFirstName function. The function should read the first word of the file.
Example file:
Bob Smith 123456789
123 Main Street
Susan Smith 1224445555
543 Market Street
Here is part of my code so far.
string file;
string first;
int main() {
ifstream inFile;
cout<<"Enter file name: ";
cin>> file;
inFile.open(file);
if(inData.fail()) {
cout<<"INVAILD";
}
getFirstName(first);
}
void getFirstName(string f, inFile file) {
file.open(f);
while(file.good(f)) {
file>>f;
}
if (file.bad()) {
cout<<"Name not found";
}
}
i will not write you the program, but let me point you in a direction.
first of all: please format your code better. maybe its just because stackoverflow, but there should be consistent and sensefull formatting of your code, otherwise you and others cant read it well and have trouble finding problems. (plz google clang-format for a tool, maybe an ide would do it as well).
split your programm in different logical parts. my approach would be:
open file, if not possible give warning, end program
read file content into a string IN: filename, OUT: string of content
split the string into line, IN: string, OUT: std::vector
for each line, split the line into parts(3 elements?) IN: linestring, OUT: name, street, ...
You can that in proper datastructures, plz see the STL for good ones
Access your datastructure for the wanted information.
Proposal for your data.
istream for file, you have that
std::string for file content
std::vector for line wise filecontent
std::unordered_map map the name to information.
this should give you a googling start. each subproblem can be solved independent (and tested) and is easier to find on SO :)
Good luck.
I agree with the comments. Yet to answer your issue "The function should read the first word of the file." below is a general idea how you can read the first word from file:
...
ifstream inFile; // define your input stream
string firstwrd; // first word
inFile.open("yourfile"); // open your file
inFile>> firstwrd; // this will read the first word
inFile.close(); // close the file
...
And please consider reading through Jonas`s comments.
Edit: Please note my answer by no means is the definitive tutorial of best practices of reading from file. It applies your specific case.
HTH!
Before you can even begin to start programming, you have to identify the exact format of the file you want to read. That format gives you the order of operations for how you intend to read from the file.
In your example, you give:
Bob Smith 123456789
123 Main Street
Susan Smith 1224445555
543 Market Street
Which is broken down into a by-line format of:
[First Name] [Last Name] [User ID (I assume)]\n
[Address]
So now that we have that established, the first thing we do is open the file stream.
ifstream file("path\\to\\file");
When it comes to retrieving information from a file stream, there are 2 standard methods: the >> operator and getline().
The >> operator returns the very next block of text in a given fstream up to any whitespace character such as space, newline or return characters. The syntax for this is file >> var where file is the fstream you intend you read from and var is the variable you want to write to.
The getline() function will return the entire line, including spaces, but will stop at return and newline characters. The actual syntax of the function is std::getline(read, write); where read is the file stream or string you intend to actually read from and write is the variable you intend to copy the real line to.
For example:
ifstream file("file.txt");
string firstname, lastname, id, address;
file >> firstname; //get the first word of the file.
file >> lastname; //get the second word of the file.
file >> id; //get the third word of the file.
getline(file, address); //Get the next whole line of the file, regardless of how many words.
A funny quirk is that you don't have to worry about manually telling C++ where in the file you're wanting to look for the data. As the file is read a pointer is automatically kept inside of the file stream of where to begin reading from next. When you get one word, the pointer automatically starts at the beginning of the next word, so you just keep pulling data linearly until you reach the end of the file.
void getFirstName(string f, inFile file); should be void getFirstName(string f, ifstream inFile);.
And remember, put an ampersand (&) between the type of the variable and the name to avoid creating a copy of the file (that consumes more ram), not putting the ampersand is only reasonable to use if you want to make changes to the variable that should not stick around.
And where is string f defined? You call the function without passing f but it's used in the function. That is a serious problem.
The ifstream.good() function if I remember correctly can't take parameters.
If you're trying to find f (you didn't tell us what it is so I can't be more precise) then you should first understand that f should be a file (since you used file.open(f);), pass the value to a string, and after doing inFile >> name_of_your_string; do if (name_of_your_string == f) { /* the last word read corresponds to f */}.
The type is ifstream, and the variable's name is inFile.
Also since getFirstName is defined after main, you got to put this before main void getFirstName(string f, ifstream inFile); this is called a prototype, and it tells the compiler that the function is after main.
Obviously don't remove the rest of the function. If that's a problem for you move main under it. Also if someone puts a space in the input everything before the last space will be lost, remove cin >> file; and use getline (cin, file);
Remember to update the answer with more details on what f is and what exactly you want it to do.
EDIT: Remember to use inFile.close(); after you stop reading the file to avoid subtle errors.

How should I go about reading in a text file with each line containing various data types in C++?

I have a textfile with lots of data. Each row has two integers which specify coordinates, followed by the name of the coordinate, and additional attributes.
I'm trying to read in all the attributes into a vector and then later access the different attributes and each row.
I'm using getline to read in each line, but how would I be able to access the various attributes instead of the whole line?
Here is my code:
while (getline(location_file, line)) {
vector<string> file;
file.push_back(line);
}
Would structs be a better option?
Instead of using getline to read the whole line, since the input seems to be standard, you can use a std::istream and the >> operator to read formatted input that ignores whitespaces. You basically have to read 2 integers and then use getline to read the rest of the line.

Creating a .txt file in C++ with variables

So I'm designing a game where data is stored and read with text files. But I need to be able to do this in a repeating fashioned so the names of the text files need to be different for each one procedurally. I figured that using variables as the name would solve this issue but I am having a hard time doing this and getting the compiler to work with it.
This is my current code to write 'hello' to a text file named 'test.txt':
ofstream myfile;
myfile.open("test.txt");
myfile << "hello";
myfile.close();
Now, I can replace "test.txt" with a single char* pointer or char array buffer, but I need to separate the name of the file with the .txt extension.
The idea that I had was to somehow combine a char* variable for a name reference (Such as galaxy, star, planet, etc.), plus a numeric value (would have to be a big number, perhaps a double or float), plus the ".txt" text extension. This would then be one char pointer or array that would be compatible with myfile.open. Any ideas? I'm open to changing the process of this as long as I get the same end result.
You should consider string instead of char* for your filename, as C++11 allows both types:
string myfilename="test";
myfile.open(myfilename+".txt");
myfile << "hello";
myfile.close();
To create more complex filenames you could then consider to use stringstreams: you can easily combine and format the filename using the usual output fromatting unctions/operators, and convert it to a string.
stringstream nn;
int counter=0;
nn<<myfilename<<counter<<".txt";
myfile.open(nn.str());
...

Loading a Linux formatted text file using a C++ windows application

My problem is I am trying to load a Linux formatted text file and read it regularly as is one was opening up a windows formatted text file in a C++ application. I have gotten it to work perfectly when the file is formatted exactly how I want it to be in windows and have the data form the file loaded into a list of lists.
To try to describe my problem a little better what I am currently able to do right now is if I have a file which is tab delimited I am able to store all of the contents from each row into a list of strings where each string is whatever each tab is separating. I then have a list of all of the rows.
For example my text file I'm reading my look something like this:
156 Hit 83.2 23:34
23 Miss 21.4 23:38
and so on....
This code spinet below is what I have been using, which I had found help elsewhere and altered it to work how I needed it to. It will create a list with two items in the list where each of the items contains a list of 4 strings each string representing the contents in each of "columns" for the current row. Hope this is explained thorough enough.
ifstream infile(file);
list <list <string> > data;
while (infile){
string s;
if (!getline( infile, s )) break;
std::istringstream ss ( s );
list <string> record;
while (ss){
string s;
if (!getline( ss, s, '\t' )) break;
record.push_back( s );
}
data.push_back( record );
}
That is exactly what I would like to do however instead of the text file I would be reading from being formatted as a Windows text file it will be a Linux text file and will not have a tab in-between each "item" in each row; but instead will contain a random number of spaces. My thought was I could open the file up in binary mode and read it that way and instead of having a tab be my delimiter I could choose any amount of white space. However I am not exactly sure how to do that as I am still relatively new to C++ and have not specifically worked with reading Linux formatted text files from a Windows C++ application. Any help would be greatly appreciated. Thanks in advance!
This has nothing to do with Linux versus Windows. You can use >> to perform formatted input of whitespace-separated fields:
string s;
while (ss >> s)
record.push_back(s);
To skip whitespace explicitly, use std::ws; to disable whitespace skipping, use std::noskipws.