【问题标题】:C++ application which reads file name from command line, use read() method to read the file's content从命令行读取文件名的 C++ 应用程序,使用 read() 方法读取文件的内容
【发布时间】:2020-05-26 18:44:27
【问题描述】:

我有这段代码,但是从命令行输入名称后它开始工作,但输出是一个永无止境的奇怪字符数组。 我试图提出的问题是:编写一个使用 read() 方法读取文件内容的 C++ 应用程序。获得的数据显示在屏幕上。每次读取操作后检查系统状态。文件名是从命令行读取的。 我的代码是:

#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;

}

我也检查了,文件没有创建.. 如果您有任何提示如何纠正它或我如何以其他方式制作它会有所帮助..

【问题讨论】:

  • 如果file.good() 为假,您将尝试打印一个非空终止的字符串...
  • @NateEldredge 我试图在那里显示一条简单的消息,输出是一样的..
  • 并且您总是在数组中的第十个字符之后终止,而不是查看读取的实际字符数。文件内容是否至少 10 个字符,且长度为 10 个字符的倍数?
  • 不管你应该修复那个错误。它迟早会得到你。
  • 注意:文件以某种不是 EOF 的方式失败,你有一个无限循环。很难处理未格式化的读取,但仍然值得为自己辩护。

标签: c++ file command-line


【解决方案1】:

这个版本正在运行, 看看我佣人的变化。

第一

我在尝试打开之前检查是否有路径

用 if_open 检查文件是否打开

编辑 将读取改为std::getline

编辑

std::ios::out添加到打开模式,如果文件不存在则创建文件

#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;

}

另外,不要使用using namespace std

【讨论】:

  • 这解决了我之前标记的问题,但提问者的指示是编写一个使用 read() 方法读取文件内容的 C++ 应用程序。getline 是在大多数情况下,这是一个好得多的主意,任务似乎禁止做聪明的事情。
  • 我也试过这段代码,现在输出只是消息“文件未创建”..我是在命令行做错了什么,还是我不明白为什么不工作。从调试属性我去了调试,我在命令行参数框中输入了一个名称
  • 如果文件不存在,打开文件不会创建文件。如果你想创建文件也打开它进行写入,
【解决方案2】:

好吧,看来我可以解决所有的错误。所以这里是好的代码:

#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;

}

问题似乎是我从未在文件中输入任何内容

【讨论】:

    猜你喜欢
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多