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

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.

Related

C++ getline in for loop

I learn to program in c++ with previous experience with python and R. Id say I understand for loops well, but now I found out that I do not know nothig about them. Here is piece of code.
for (int i = 0; i != 1; ){
string name;
getline(infile, name);
if (name == end_input){
i = 1;
}
else{
names.push_back(name);
}
}
Whole program should (and do) read names (name) from file infile and store them into names string. Than I want them to store in another file. When I look on the code, I would thing c++ do following instructions:
create integer i and set it to 0
create string name
read the line from infile and store this line into names string vector.
this will repeat unless name == end_input
From this I would say that c++ will store first line in input file again and again because I didnt tell him to jump to next line after getline the first line. But program reads all names from that file, line by line as expected by author. How is that possible?
Thank you.
getline automatically moves to the next line after reading a line.
Also a do while loop might serve your purposes better here.
When an inbuilt function does not behave as you expected, the logical next step should be to check the Documentation. If you do, you will see the following:
Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, '\n', for (2)).
The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.
If the delimiter is found, it is extracted and discarded (i.e. it is not stored and the next input operation will begin after it).
Which answers your Question.

C++ Easy way to ignore first word of input

I am writing a program to read a text file line by line, store the line values in a vector, do some processing then write back to a new text file. This is what the text file typically looks like:
As you can see, there are two columns: one for the frame number and another for the time. What I want is only the second column (aka the time). There can be hundreds, if not thousands of lines in the text file. Previously I have been manually deleting the frame number column which i'd rather not do. So my question is: is there an easy way to edit my current code so that when I read the file with getline() it skips the first word and only gets the second? Here is the code that I use to read the text file. Thanks
ifstream sysfile(sys_time_dir);
//Store lines in a vector
vector<string> sys_times;
string textline;
while (getline(sysfile, textline))
{
sys_times.push_back(textline);
}
Since you have two numbers in each line, you can read two numbers and ignore the first number.
vector<double> sys_times;
int first;
double second;
while ( sysfile >> first >> second )
{
sys_times.push_back(second);
}
std::string ignore_me;
while (sysfile >> ignore_me, getline(sysfile, textline)) {
...
This utilizes the comma operator, reading in the first word (here defining "word" as a continuous sequence of non-space characters) of the line, but ignoring the result, then using getline to read the rest of the line.
Note that for the specific data format you describe, I would rather choose what RSahu showed in their answer. My answer is more general to the problem of "skipping the first word and reading the rest of the line".

Having an input that already have a text in it in c++

Just a question, is there anyway that I can have an input with a text already written for me with using "cin." I'm looking for a predefined function or whatever that I can use to have inputs with a text in it. My purpose of that is that I'm trying to make a simple text editor in command-line. Even if there aren't any predefined functions, anyone have a pseudocode to copy a string of words into a cin (or any other input). I hope that my question is not vague pointless.
Regards.
You can assign the buffer of std::cin to read from a file on disk instead of the terminal, like this:
ifstream file("file.txt");
cin.rdbuf(file.rdbuf());
From then on, when you read from cin it will give you the contents of file.txt.
If you prefer you can also make std::cin produce text directly from a string, like this:
istringstream stream("blah\nblah blah");
cin.rdbuf(stream.rdbuf());
Now when you read from cin you will get two lines: blah and blah blah.

C++ reading from a multi-type csv file

I'm trying to use C++ to read from a file, which is structured like this:
frank, 80, 90
johnny, 10, 25
...
I made a loop to go through each line of the file, but every time I read the name string, the comma is included with the name (so instead of frank I get frank,).
My code in question is:
// var declarations
ifstream streamVar;
string name;
int num1, num2;
// there is a chunk of code that opens file and does error checking here
// this is the code that I'm having trouble with
streamVar >> name;
streamVar >> num1;
streamVar.ignore(100, ',');
streamVar >> num2;
How can I read these three values while ignoring the commas?
IIRC the streams use space as a default delimiter and you will have to specify that ',' is another delimiter.
Here's another question on SO regarding specifying delimiters, that should solve your problem.
Must be because your code is wrong. Exactly how is anyone's guess since you failed to post it.
I've posted the code I use to read and write CSV files in C++. Perhaps that will be some help.

How do you read a word in from a file in C++?

So I was feeling bored and decided I wanted to make a hangman game. I did an assignment like this back in high school when I first took C++. But this was before I even too geometry, so unfortunately I didn't do well in any way shape or form in it, and after the semester I trashed everything in a fit of rage.
I'm looking to make a txt document and just throw in a whole bunch of words
(ie:
test
love
hungery
flummuxed
discombobulated
pie
awkward
you
get
the
idea
)
So here's my question:
How do I get C++ to read a random word from the document?
I have a feeling #include<ctime> will be needed, as well as srand(time(0)); to get some kind of pseudorandom choice...but I haven't the foggiest on how to have a random word taken from a file...any suggestions?
Thanks ahead of time!
Here's a rough sketch, assuming that the words are separated by whitespaces (space, tab, newline, etc):
vector<string> words;
ifstream in("words.txt");
while(in) {
string word;
in >> word;
words.push_back(word);
}
string r=words[rand()%words.size()];
The operator >> used on a string will read 1 (white) space separated word from a stream.
So the question is do you want to read the file each time you pick a word or do you want to load the file into memory and then pick up the word from a memory structure. Without more information I can only guess.
Pick a Word from a file:
// Note a an ifstream is also an istream.
std::string pickWordFromAStream(std::istream& s,std::size_t pos)
{
std::istream_iterator<std::string> iter(s);
for(;pos;--pos)
{ ++iter;
}
// This code assumes that pos is smaller or equal to
// the number of words in the file
return *iter;
}
Load a file into memory:
void loadStreamIntoVector(std::istream& s,std::vector<std::string> words)
{
std::copy(std::istream_iterator<std::string>(s),
std::istream_iterator<std::string>(),
std::back_inserter(words)
);
}
Generating a random number should be easy enough. Assuming you only want psudo-random.
I would recommend creating a plain text file (.txt) in Notepad and using the standard C file APIs (fopen(), and fread()) to read from it. You can use fgets() to read each line one at a time.
Once you have your plain text file, just read each line into an array and then randomly choose an entry in the array using the method you've suggested above.