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

C Programming How do you read in a text file line by line into a an...
C Programming How do you read in a text file line by line into a an array. example, i have the text file containing: line 1: "qwertyuiop" line 2: "asdfghjkl" line 3: "zxcvbnm" Then in the resulting array i get this: array:"qwertyuiopasdfghjklzxcvbnm"
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...
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...
Code is only reading the last line from the text files. How would I resolve? This...
Code is only reading the last line from the text files. How would I resolve? This is for the Name Search problem in Starting out with Python book. def main(): #open a file for reading infile = open('GirlNames.txt' , 'r') #Read the contents into a list Girls = infile.readlines() #open another file for reading infile = open('BoyNames.txt', 'r') #Read the contents into a lits Boys = infile.readlines() #Get a name to search for Name = input('Enter a name: ') #Determine...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such as: "name,23" on every line where a comma is the delimiter, how would I separate the string "name" and integer "23" and set them to separate variables for every input of the text file? I am trying to set a name and age to an object and insert it into a vector. #include <iostream> #include <vector> #include <fstream> using namespace std; class Student{    ...
so i want to read in a file. just to keep it simple, the text file...
so i want to read in a file. just to keep it simple, the text file has student name and the grade, separated by a space. let's say there are 192 student's name and I want to read them in and made them into a Student /object class, which has a constructor, student(string name, int grade). individually creating them seems pain in the ass. reading in should work for any number of names. So how do I do this in...
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
Write a C program to run on unix to read a text file and print it...
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring] filename • The -c flag...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT