I have this code but after entering the name from the command line it starts working but the output is a never ending weird character array. The problem which i tried to make says: Write a C++ application that reads a file’s content using the read() method. The obtained data is displayed on the screen. Check the system’s state after each reading operation. The filename is read from the command line. My code is:
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc, char* argv[])
{
char arr[15];
ifstream file;
file.open(argv[1], ios::in); //filename read from the command line
if (argc == 1)
{
cout << "Enter file name from command line!";
}
int readState = 0;
while (!file.eof())
{
file.read(arr, 10); //reading the file's content
if (file.good())
{
arr[10] = '\0';
cout << arr;
readState++; //checking the system's state after each read()
}
else
{
cout << arr;
}
}
file.close();
return 0;
}
I also checked and the file is not created.. If you have any tips how to correct it or how could i make it in some other way it would help..