0

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..

vivi25-5
  • 55
  • 5
  • If `file.good()` is false, you attempt to print a string that is not null terminated... – Nate Eldredge May 26 '20 at 18:47
  • @NateEldredge i tried to display a simple message there and the output is the same.. – vivi25-5 May 26 '20 at 18:49
  • and you always terminate after the tenth char in the array, not looking at the actual number of characters read. Is the file content at least 10 chars, and multiples of 10 chars long? – Cee McSharpface May 26 '20 at 18:50
  • Regardless you ought to fix that bug. It'll get you sooner or later. – user4581301 May 26 '20 at 18:51
  • Note: Of the file fails in some way that is NOT an EOF, you've got an infinite loop. Hard to do with unformatted reads, but still it's worth defending yourself. – user4581301 May 26 '20 at 18:54
  • Checking that the file opened can be done with `is_open`. If the file failed to open you'll have to use target-specific calls to find out why. If you suspect the file doesn't exist, you can use the [`` library](https://en.cppreference.com/w/cpp/filesystem) added in c++17 to take a look for yourself. – user4581301 May 26 '20 at 18:58

2 Answers2

1

this version is working, look at the changes I maid.

first

I'm checking if I got a path before trying to open it

second

check if the file open with if_open

EDIT change the read to std::getline

EDIT

add std::ios::out to the open mode to create the file if it doesn't exist

#include <stdio.h>
#include <iostream>

#include<iostream>
#include<fstream>
using namespace std;

int main(int argc, char* argv[])
{
  char arr[15];
  ifstream file;
  if (argc == 1)// <- moved it before opening the file
  {
    cout << "Enter file name from command line!";
    return 0;
  }
  file.open(argv[1], ios::in|std::ios::out); //filename read from the command line and also create the file if it dosent exist
  if(!file.is_open()) // <- second change
  {
    std::cout << "file not opening\n";
    return 0;
  }

  int readState = 0;
  std::string line;
  while ( std::getline(file, string ) ) // <-- third fix
  {
    cout << arr;
    readState++; //checking the system's state after each read()
    }
    else
    {
      cout << arr;
    }
  }
  file.close();
  return 0;

}

also, dont use using namespace std

yaodav
  • 1,126
  • 12
  • 34
  • That fixes the problem I flagged earlier, but the questioner's instructions are to **Write a C++ application that reads a file’s content using the read() method.** While `getline` is a much, much better idea in most cases, the assignment seems to forbid doing the smart thing. – user4581301 May 26 '20 at 22:54
  • I tried this code as well and now the output is just the message "File is not created".. am i doing something wrong with the command line or i don't get why's not working. From Debug Properties i went to Debugging and i entered a name in the Command Line arguments box – vivi25-5 May 27 '20 at 07:02
  • the opening of the file doesn't create the file if it doesn't exist. if you want to create the file also open it for writing, – yaodav May 27 '20 at 07:12
0

Okay it looks like i could solve all the bugs. So here's the good code:

#include<iostream>
#include<fstream>
using namespace std;

int main(int argc, char* argv[]) //using command line arguments
{
    char arr[15];
    ofstream wfile;
    if (argc == 1)
    {
        cout << "Enter file name from command line!";
        return 0;
    }

    cout << "Enter an array:";
    cin >> arr; //reading the array from KBD

    wfile.open(argv[1], ios::out);  //filename read from the command line
    if (!wfile)
    {
        cout << "File can't be opened!";
        exit(1);
    }
    else
        cout << "File created" << endl;
    wfile.write(arr, strlen(arr)); //write the array into file
    wfile.close(); //closing file 

    ifstream file;
    file.open(argv[1], ios::in); //opening the file to read from it
    if (!file.is_open()) //if file is not opened displays a message
    {
        cout << "File not opening\n";
        exit(1);
    }

    int readState = 0; //initializing the system state
    char *array;
    array = new char[10]; //allocating memory

    while (!file.eof())
    {
        file.read(array, 10); //using read() method
        for(int i=0;i<10;i++)
            cout << array[i]; //display the array from the file
        readState++;
    }

    cout <<"\nSystem state is:"<< readState; //checking system state
    delete[]array; //eliberating memory
    file.close(); //closing file
    return 0;

}

It seems that the problem was that i never entered anything in the file

vivi25-5
  • 55
  • 5