输出文件不可读,这是一个错误吗?

时间:2016-01-01 14:20:01

标签: c++

这是我的代码:

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

char filename[100];
FILE *stream, *stream2;
char s[20];
struct date
{
    int day, month, year;
};
struct employee
{
    int ID;
    char name[100];
    date birthdate;
    char address[20];
    char rank[20];
    int money;
};

void main()
{
    errno_t err;
    // Open for read (will fail if file "crt_fopen_s.c" does not exist)

    // Open for write 
    err = fopen_s(&stream2, "C:/Users/Van/Desktop/LAALAL/fool.txt", "w+");
    if (err == 0)
    {
        employee nv;
        std::cout << "\nInput information of an employee:\n";
        std::cout << "\tInput ID : ";
        std::cin >> nv.ID;
        std::cin.sync();
        std::cout << "\tInput name : ";
        std::cin.clear();
        gets_s(s);
        gets_s(nv.name);
        std::cout << "\tInput birthdate (Day Month Year ) : ";
        std::cin >> nv.birthdate.day >> nv.birthdate.month >> nv.birthdate.year;

        std::cout << "\tInput address: ";
        std::cin.clear();
        gets_s(s);
        gets_s(nv.address);
        std::cout << "\tInput rank : ";
        std::cin.clear();
        gets_s(s);
        gets_s(nv.rank);
        std::cout << "\tMoney : ";
        std::cin >> nv.money;
        std::cin.sync();

        std::fwrite(&nv, sizeof(nv), 1, stream2);
        std::fclose(stream2);
    }
}

我对代码没有任何问题,但是当我输入我的信息时,我无法读取文件中的输出。这是我输出的图片:

enter image description here

我的问题是什么? 提前感谢您的时间!

2 个答案:

答案 0 :(得分:1)

您正在使用fwrite()功能,该功能将数据作为二进制写入文件,而不是文本(ASCII)。您应该使用std::ofstream中的<fstream>类(而不是FILE)和<<运算符。

更多信息: http://www.cplusplus.com/doc/tutorial/files/

示例代码:

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

struct some_struct{
    int some_int;
    char some_char;
};

int main () {
    struct some_struct x;
    x.some_int = 123123;
    x.some_char = 'x';

    //This is how you open the file.
    ofstream myfile; 
    myfile.open ("example.txt");

    //This is is how you write to it.
    myfile << "Integer: " << x.some_int << " Char: " << x.some_char;

    //This is how you close it.
    myfile.close();
    return 0;
}

文件example.txt内的输出:

Integer: 123123 Char: x

答案 1 :(得分:0)

您可以使用 fstream 库进行编程,例如&lt;&lt; &gt;&gt;

#include <iostream>
#include <fstream>

using namespace std;
struct date
{
    int day, month, year;
};

struct employee
{
    int ID;
    char name[100];
    date birthdate;
    char address[20];
    char rank[20];
    int money;
};

int main()
{
    char ch;
    ofstream file("fool.txt");
    if (!file)
    {
        cout << ""Cannot open file, press any key to continue ...";
        cin.get();
        exit(0);
    }
    employee nv;
    cout << "\nInput information of an employee:\n";
    cout << "\tInput ID : ";
    cin >> nv.ID;
    cin.ignore();
    cout << "\tInput name : ";
    gets_s(nv.name);
    cout << "\tInput birthdate (Day Month Year) : ";
    cin >> nv.birthdate.day >> nv.birthdate.month >> nv.birthdate.year;
    cin.ignore();
    cout << "\tInput address: ";
    gets_s(nv.address);
    cout << "\tInput rank: ";
    gets_s(nv.rank);
    cout << "\tMoney: ";
    cin >> nv.money;
    cin.ignore();
    file << nv.ID << ' ' << nv.name << ' ' << nv.birthdate.day 
        << ' ' << nv.birthdate.month << ' ' << nv.birthdate.year 
        << ' ' << nv.address << ' ' << nv.rank << ' ' << nv.money << ' ';
    file.close();


    cout << "\nOutput From File : \n";
    ifstream file2("fool.txt");
    file2.get(ch);
    while (!file2.eof())
    {
        cout.put(ch);
        file2.get(ch);
    }
    file2.close();
    cout << "\nOutput Completed";
    cin.get();
}

并注意这些提示:

  1. 首先在代码中声明std命名空间的用法,以便删除std::子句。

    using namespace std;
    
  2. cin.clear()cin.sync()不是必需的,只需在输入整数类型之后使用cin.ignore()并输入char类型

    cin >> nv.ID;
    cin.ignore();
    gets_s(nv.name);
    

    请参阅this post以便更好地了解

  3. 您正在从某些行的输入中收集char s[20];。为什么??将其删除

  4. 在此示例中并使用运算符进行文件编程(<<),您应该在将每个数据写入文件后添加空格字符以获取单独的数据

    file << nv.ID << ' ' << nv.name
    
  5. 输入或打印字符类型的标准函数是gets()puts(),但您使用的gets_s()我认为是因为安全警告,您可以禁用此错误,在这种情况下,如果您希望您的代码使用其他编译器,那么这将不是问题。您只能评论一行而不是更改多行

    #pragma warning(disable: 4996)