C++ fstream跳过第一个字符

C++ fstream跳过第一个字符,c++,fstream,C++,Fstream,我试图从文本文件中读取结构的前121个字节 这是我的密码 #include <fstream.h> #include <iostream.h> #include <conio.h> #include <sys/stat.h> int main() { struct { char map[121]; } map_data; struct stat results; fstream myf

我试图从文本文件中读取结构的前121个字节

这是我的密码

#include <fstream.h>
#include <iostream.h>
#include <conio.h>
#include <sys/stat.h>

int main()
{
    struct 
    {
        char map[121];
    } map_data;

    struct stat results;

    fstream myfile("input.txt", ios::in);

    myfile.read((char *)&map_data,121);

    if(!myfile)
    {
           cout<<"Unable to open the file";
    }
    if(!myfile.read((char *)&map_data,121))
    {
       cout<<"Second error occurred";
    }

    myfile.close();
    cout<<"\n Here are the read contents of size "<<sizeof(map_data)<<"\n";



    fstream outfile("output.txt", ios::out);
    for(int i=0;i<121;i++)
    {
        cout<<map_data.map[i]<<" ";
    }
    outfile.write((char *)&map_data,121);


    outfile.close();
    stat("input.txt",&results);
    cout<<"\n Size of input.txt "<<results.st_size;

    stat("output.txt",&results);
    cout<<"\n Size of output.txt "<<results.st_size;

    getch();

}
#包括
#包括
#包括
#包括
int main()
{
结构
{
字符映射[121];
}地图数据;
结构统计结果;
fstream myfile(“input.txt”,ios::in);
myfile.read((char*)&map_数据,121);
如果(!myfile)
{

cout我有点惊讶,只有第一个字符丢失了。它应该少了121个字符

fstream myfile("input.txt", ios::in);

// you read 121 bytes here...
myfile.read((char *)&map_data,121);

if (!myfile) {
    cout<<"Unable to open the file";
}

// and then do it again here, before you ever look at `map_data`.
if (!myfile.read((char *)&map_data,121)) {
    cout<<"Second error occurred";
}
fstream-myfile(“input.txt”,ios::in);
//你在这里读了121个字节。。。
myfile.read((char*)&map_数据,121);
如果(!myfile){

cout我有点惊讶,只有第一个字符丢失了。它应该少了121个字符

fstream myfile("input.txt", ios::in);

// you read 121 bytes here...
myfile.read((char *)&map_data,121);

if (!myfile) {
    cout<<"Unable to open the file";
}

// and then do it again here, before you ever look at `map_data`.
if (!myfile.read((char *)&map_data,121)) {
    cout<<"Second error occurred";
}
fstream-myfile(“input.txt”,ios::in);
//你在这里读了121个字节。。。
myfile.read((char*)&map_数据,121);
如果(!myfile){
我是按照向导做的吗

该示例显示了两种不同的读取文件的方法,注意示例之间的a

它还说
//与上面的效果相同

所以只需注释掉两个read调用中的任何一个

    fstream myfile("input.txt", ios::in);

    //myfile.read((char *)&map_data,121);

    if(!myfile.read((char *)&map_data,121))
    {
       cout<<"Second error occurred";
    }
fstream-myfile(“input.txt”,ios::in);
//myfile.read((char*)&map_数据,121);
if(!myfile.read((char*)&映射_数据,121))
{
我是按照向导做的吗

该示例显示了两种不同的读取文件的方法,注意示例之间的a

它还说
//与上面的效果相同

所以只需注释掉两个read调用中的任何一个

    fstream myfile("input.txt", ios::in);

    //myfile.read((char *)&map_data,121);

    if(!myfile.read((char *)&map_data,121))
    {
       cout<<"Second error occurred";
    }
fstream-myfile(“input.txt”,ios::in);
//myfile.read((char*)&map_数据,121);
if(!myfile.read((char*)&映射_数据,121))
{

事实上,我正在使用它进行一个项目,有人写了大约10年前。我不想把它移植到新的项目中。请不要再在这里粘贴这样一个糟糕的代码样式。我试图编辑…它有一个糟糕的缩进样式,可能是复制粘贴的最终结果…@Laszlo Papp我将每行缩进四个空格,以便显示为代码。还有什么要求吗?为什么要读同一个文件两次?它适用于我和Mingw。(在更改了标题和几个语句之后)问题解决了。非常感谢POW。问题是读了两次文件。我对再次阅读文件的第二个区块进行了评论,它成功了。非常感谢refp和Laszlo Papp编辑我的帖子。实际上,我正在使用它进行一个项目,有人在近10年前写了回信。我不想将它移植到新的。请不要这样做这里再也没有这么糟糕的代码样式了。我试图编辑…它有一个糟糕的缩进样式,可能是复制粘贴的最终结果…@Laszlo Papp我按照要求将每行缩进四个空格,以使其显示为代码。还有什么要求?你为什么要读同一个文件两次?这对我来说适用于Mingw。(更改标题和少数语句后)问题解决了。非常感谢POW。问题是我读了两次文件。我对我再次阅读文件的第二个区块进行了评论,它成功了。非常感谢refp和Laszlo Papp编辑我的文章。谢谢cHao。我找到了答案。你能发布这个吗?我不能。我读了两次文件。我对我正在阅读的第二个区块进行了评论再次阅读文件,一切都很好。这不是问题。对不起,我犯了错误。我遵循了指南。谢谢赵。我找到了答案。你能发布这个吗?我不能。我阅读了两次文件。我对再次阅读文件的第二个区块进行了注释,一切都很好。这不是问题。对不起,我的错误。我遵循了指南。