Question

In: Computer Science

How would I read only the first line of text file into C++ For instance, the...

How would I read only the first line of text file into C++

For instance, the first line of a text file is:

5 (space) 5.

I need to read these numbers into a row variable and a column variable. I am just not sure how to do it.

I can only use the #include header. I can't use any other header.

The project mentions using redirected input in Visual Studio for our text file.

Solutions

Expert Solution

Method 1
===================================================

The following code shows how to read an input file using ifstream.

input.txt
--------
5 5


main.cpp
--------

#include <iostream>
#include <fstream>
using namespace std;
int main(){
string filename = "input.txt";
ifstream infile(filename.c_str());

if(infile.fail()){
cout << "ERROR: could not open input file " << filename << endl;
return 0;
}

int row, col;
infile >> row >> col;

cout << "Row = " << row << ", Col = " << col << endl;
}

output
=====

Row = 5, Col = 5

===================================================

Method 2: Using file redirection (mostly used to avoid retyping inputs to program)
===================================================
Here you only use file redirection to provide all the inputs to your program. So you basically avoid typing in again and again. Your program will only use cin, but due to file redirection, instead of you typing in when prompted, the values come from the file.
Once you have a executable program .exe, you use the command (left arrow is the redirection operator)

executable_file_name < input_filename

input.txt
--------
5 5


main.cpp
=====
#include <iostream>
using namespace std;
int main(){
int row, col;
cout << "Enter row and column values: ";
cin >> row >> col;

cout << "Row = " << row << ", Col = " << col << endl;
}

See the below screen shot. You will see I did not type in values, but the values were read from file redirection.


Related Solutions

For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
Assignment in C programming class: read the text file line by line, and place each word,...
Assignment in C programming class: read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a...
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
Write a simple phonebook using c++ that read text file name list.txt and has:  first name, last...
Write a simple phonebook using c++ that read text file name list.txt and has:  first name, last name, and phone number as the example: MIKEL BUETTNER 3545044 ENOCH BUGG 2856220 BRENDON LAVALLEY 433312 QUINTIN CREEK 5200413 JAMISON MILLETT 6243458 FLORENCIO PUMPHREY 3296862 DARRICK FREGOSO 6868442 TOBIAS GLASSMAN 6040564 and then ask the user to add a new contact first name, last name, and phone number and same the information into a file. Use array and pointer
Whats the code to open a text file and every line in that text file that...
Whats the code to open a text file and every line in that text file that starts with # then it should delete that line In python using .strip
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
In python explain why would we want to read a file line by line instead of...
In python explain why would we want to read a file line by line instead of all at once?
Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT