当我尝试读取 2 个 txt 文件时,我的输出中显示了很多 0

第 44 届

对不起,这很长,但我不知道我应该怎么做才能解决这个问题

对于输出的 0,我猜测它是另一个文件的内容,但在 0 中我只是不知道如何摆脱它们

这是我的代码:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct Users
{
    int user_id;
    string fname;
    string lname;
    char gender;
    int age;
    int phone;
    string address;
};

struct Contacts
{
    int user_id;
    int contact_with;
    int contact_start;
    int contact_end;
    int distance;
};

void discard_line(ifstream &in)
{
    char c;

    do{
        in.get(c);
    }while(c != '\n');
}

void users(ifstream &userFile, int size, struct Users user[])
{
    cout << "-----------------------------------------------------\n";
            cout << "UserID\tFname\t Lname\tGender\tAge\tPhone\tAddress" << endl;
            cout << "-----------------------------------------------------\n";
            for(int i = 0; i < size; i++)
            {
                cout << user[i].user_id << "\t" << user[i].fname << "\t" << user[i].lname << "\t" << user[i].gender << "\t" << user[i].age << "\t" << user[i].phone << "\t" << user[i].address << endl;
            }
            //total number of users
}

void contacts(ifstream &contactfile, int size, struct Contacts contact[])
{
    cout << "-----------------------------------------------------\n";
            cout << "UserID\tCon/With\tDuration(s)\tDistance(cm)" << endl;
            cout << "-----------------------------------------------------\n";
            for(int i = 0; i < size; i++)
            {
                int duration = contact[i].contact_end - contact[i].contact_start;
                cout << contact[i].user_id << "\t" << contact[i].contact_with << "\t\t" << duration << "\t\t" << contact[i].distance << endl;
            }
}

int main()
{
    int option;
    const int SIZE = 1000;
    int index = 0;
    Users user[SIZE];
    Contacts contact[SIZE];

    ifstream userFile("users.txt");
    ifstream contactFile("contacts.txt");

    if(!userFile)
    {
        cout << "The file is not found" << endl;
        exit(1);
    }

    if(!contactFile)
    {
        cout << "The file is not found" << endl;
        exit(1);
    }

    discard_line(userFile);
    discard_line(contactFile);

    while(!userFile.eof())
    {
        userFile >> user[index].user_id >> user[index].fname >> user[index].lname >> user[index].gender >> user[index].age >> user[index].phone >> user[index].address;
        index++;
    }

    while(!contactFile.eof())
    {
        contactFile >> contact[index].user_id >> contact[index].contact_with >> contact[index].contact_start >> contact[index].contact_end >> contact[index].distance;
        index++;
    }

    users(userFile,index,user);
    contacts(contactFile,index,contact);
        
    return 0;
}

输出是这样的:

    -----------------------------------------------------
UserID  Fname    Lname  Gender  Age     Phone   Address
-----------------------------------------------------
1001    Ray     Dixon   M       46      9364652 Lokia
1002    Bryan   Green   M       18      9579302 Drekena
1003    Justin  Dixon   M       33      9353533 Lokia
1004    Lester  Byrd    M       45      9534695 Nasilai
1005    Santos  Larson  M       53      9093177 Vunuku
1006    Bryan   Cobb    M       42      9905139 Narocivo
1007    Eddie   Watson  M       20      9610408 Nabua
1008    Wesley  Barton  M       27      9801864 Nasigatoka
1009    Victor  Mason   M       50      9855386 Nukutubu
1010    Ellis   Cobb    M       24      9389406 Narocivo
1011    Diana   Ross    F       27      9940148 Vunuku
1012    Amanda  Carter  F       43      9506743 Nasilai
1013    Maria   Edwards F       53      9798534 Narocivo
1014    Maria   Jenkins F       34      9352516 Lomanikoro
1015    Louise  Davis   F       55      9812126 Nasilai
1016    Sandra  Sanders F       29      9369570 Tavuya
1017    Bonnie  Roberts F       40      9689234 Nukui
1018    Melissa Harris  F       29      9321235 Drekena
1019    Marilyn Parker  F       56      9409221 Nukui
1020    Bonnie  Lopez   F       43      9342939 Nasigatoka
0                               0       0
0                               0       0
0                               0       0
0                               0       0
0                               0       0
0                               0       0
0                               0       0
0                               0       0
0                               0       0
0                               0       0

我正在尝试删除 0,但我不知道该怎么做。但是,当我取出任一文件的读数时,它会显示没有 0 的正确输出

海绵宝宝
while(!userFile.eof())
{
    userFile >> user[index].user_id >> user[index].fname >> user[index].lname >> user[index].gender >> user[index].age >> user[index].phone >> user[index].address;
    index++;
}

while(!contactFile.eof())
{
    contactFile >> contact[index].user_id >> contact[index].contact_with >> contact[index].contact_start >> contact[index].contact_end >> contact[index].distance;
    index++;
}

users(userFile,index,user);
contacts(contactFile,index,contact);

假设索引从零开始。假设 userFile 有 10 行假设 contactFile 有 10 行

当您在 while(!userFile.eof()) 中填充您的用户数组时,您将索引增加到 10...但是在执行 while(!contactFile.eof()) 时,您的索引不再从零开始...而是从 10 点停止的地方开始并继续到 20 点

你需要一个额外的索引...

int uIndex = 0;
int cIndex = 0;

while(!userFile.eof())
{
    userFile >> user[uIndex].user_id >> user[uIndex].fname >> user[uIndex].lname >> user[uIndex].gender >> user[uIndex].age >> user[uIndex].phone >> user[uIndex].address;
    uIndex++;
}

while(!contactFile.eof())
{
    contactFile >> contact[cIndex].user_id >> contact[cIndex].contact_with >> contact[cIndex].contact_start >> contact[cIndex].contact_end >> contact[cIndex].distance;
    cIndex++;
}

users(userFile,uIndex,user);
contacts(contactFile,cIndex,contact);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当我尝试读取Java中的控制台输出错误时没有得到结果

读取2个.txt文件,访问其中的数据,然后打印

“ TypeError:列表索引必须是整数或切片,而不是str”当我尝试读取json时

当我尝试从2个文件创建zip时出现错误

java.io.IOException:sureRemaining:仅剩余0个字节,尝试读取1

python pandas:尝试读取txt文件,但显示为NaN

如何使用Powershell读取txt文件中2个特殊字符之间的行

我正在尝试从R中的github读取一个csv文件。但是,当我尝试获取列的名称时,我只会得到一个名称

运行时错误70权限2个程序尝试读取和写入csv文件时被拒绝

我有2个相同的ID,但是当我尝试查找它们是否相等时,它们不是

当我尝试读取此excel文件时,Python为什么只有第一个for循环起作用而第二个却不起作用?

在Linux中合并2个txt文件

当我尝试对2个数组使用scanf时出现错误

如何使用python读取多个(超过2个).txt文件

当我尝试读取通过Intent传递的Vector时,出现NullPointerException

我想从3个不同的txt文件中获取数据,然后随机显示数据

当我尝试使用 2 个表进行删除时出现 SQL 错误

当我尝试在 2 个单独的画布元素上渲染相同的图像时,Angular 6

关于我尝试读取 .FIT 文件时的异常

尝试读取 txt 文件时出现 TIC SSL 信任错误

如果我尝试读取一个不存在的文件会抛出什么异常?

尝试读取txt文件并将它们转换为具有不同原语的对象

Pandas ImportError:当我尝试读取 .csv 文件时无法导入名称“OrderedDict”

正在尝试读取population_data.json文件,但是当我运行代码时,它不会在文件中显示任何数据,我也没有收到任何错误?

在 Snowsql 中,当我尝试“放置”一个文件时,我收到以下错误消息:

每当我尝试读取字符串时,我的 C++ 程序就会崩溃

我想要一个批处理文件来删除绝对路径位置 (F:\work) 中的所有文件,除了名为“txt1.txt”和“txt2.txt”的两个文件

当我尝试读取 object[key].value 时,为什么我有一个 undefined?

当我尝试使用 .txt 文件时,.exe 程序崩溃