Print nth in C++ Print the nth character of a file. You must ask the user for a
ID: 3876711 • Letter: P
Question
Print nth in C++
Print the nth character of a file. You must ask the user for a file name and a value for n. You can assume that the file is in the current directory. You can assume that a valid value for n is given, and that indexing starts at 0.
Console:
Please enter a filename:
input_file.txt
Please enter an index:
25
The character at index 25 is:
t
input_file.txt
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Explanation / Answer
using namespace std;
int main()
{
string reading_content;
string file_name;
fstream readfile;
int index=0
cout<<"Enter file name with extension... ";
cin>>file_name;
cout<<"enter the index";
cin>>index;
readfile.open(file_name,ios_base::in);
if (!readfile)
{
cout<<"Error reading file!";
return 0;
}
else
{
cout<<"Reading data from the file... ";
getline(readfile,reading_content);
cout<<"The file contents are: ";
cout<<reading_content;
cout<<"content at the specific index";
count<<reading_content[index];
readfile.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.