c++ - 为什么此代码不输出到文件?

标签 c++ input

我调试了我的程序,数组似乎分配得很好。然而,由于一些奇怪而愚蠢的原因,代码没有将数组输出到文件中。

请帮我找出我的错误或类似的错误!

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

void sRecSort(string  *n, int *s, string *e, int len){
    for (int i = 0; i < len; i++){
        for (int j = i + 1; j < len; j++){
            if (s[j] < s[i]){
                swap(s[i],s[j]);
                swap(e[i],e[j]);
                swap(n[i],n[j]);
            }
        }
    }
}

void printLowestRecord(char inFileName[]){
    string tempSubString = " ";
    string names[12] = {" "};
    int grades[12] = {0};
    string emails[12] = {""};
    int firstSpace = -1;
    int secondSpace = -1;
    ifstream inputMe(inFileName);
    while (!inputMe.eof()){
        for (int i = 0; i < 12; i++){
            getline(inputMe, tempSubString);
            for (int w = 0; w < strlen(tempSubString.c_str()); w++){
                if (tempSubString[w] != ' '){
                    continue;
                }
                else{
                    if (firstSpace == -1){
                        firstSpace = w;
                    }
                    else if (firstSpace != -1 && secondSpace == -1){
                        secondSpace = w;
                        names[i] = tempSubString.substr(0, firstSpace);
                        grades[i] = atoi((tempSubString.substr(firstSpace + 1, secondSpace - (firstSpace + 1))).c_str());
                        emails[i] = tempSubString.substr(secondSpace + 1, tempSubString.length() - (secondSpace + 1));
                        break;

                    }
                }
            }
            firstSpace = -1;
            secondSpace = -1;
        }
    }
    sRecSort(names, grades, emails, 12);
    inputMe.close();
}

void sortFileRecords(char inFileName[], char outFileName[]){
    ifstream inputFile(inFileName);
    ofstream outputFile(outFileName);
    string tempSubString = " ";
    string names[12] = {" "};
    int grades[12] = {0};
    string emails[12] = {" "};
    int firstSpace = -1;
    int secondSpace = -1;
    while (!inputFile.eof()){
        for (int i = 0; i < 12; i++){
            getline(inputFile, tempSubString);
            for (int w = 0; w < strlen(tempSubString.c_str()); w++){
                if (tempSubString[w] != ' '){
                    continue;
                }
                else{
                    if (firstSpace == -1){
                        firstSpace = w;
                    }
                    else if (firstSpace != -1 && secondSpace == -1){
                        secondSpace = w;
                        names[i] = tempSubString.substr(0, firstSpace);
                        grades[i] = atoi((tempSubString.substr(firstSpace + 1, secondSpace - (firstSpace + 1))).c_str());
                        emails[i] = tempSubString.substr(secondSpace + 1, tempSubString.length() - (secondSpace + 1));
                        break;
                    }
                }
            }
            firstSpace = -1;
            secondSpace = -1;
        }
    }
    sRecSort(names, grades, emails, 12);

    for (int q = 0; q < 12; q++){
        outputFile << names[q] << " " << grades[q] << " " << emails[q] << endl;
    }
    inputFile.close();
    outputFile.close();
}

int main (int argc, char * const argv[]) {
    printLowestRecord("gradebook.txt");
    sortFileRecords("gradebook.txt", "sortedGradebook.txt");
    return 0;
}

这是我的数据:
肖恩 80 sean@csi.edu
詹姆斯 100 james@yahoo.com
伊萨克 99 issac@mail.csi.edu
托马斯 88 tom@cix.csi.edu
爱丽丝 78 alice@myclass.com
琼斯 75 jone@hotmail.com
扎克 89 zach@yahoo.com
马克 86 mark@gmail.com
尼克 79 nick@bmail.com
艾米 95 amy@hotmail.com
克莱尔 89 claire@yahoo.com
97 年前夕 eve@nytimes.com

最佳答案

到目前为止代码似乎是正确的,我认为你的测试数据是错误的。如果我使用这个输入文件进行测试:

a 10 c
d 2 f
g 9 i
j 4 l
m 8 o
p 6 r
s 7 u
v 8 x
y 6 a
b 10 d
e 5 g
h 12 j

输出文件是这样的,这是预期的行为:

d 2 f
j 4 l
e 5 g
y 6 a
p 6 r
s 7 u
m 8 o
v 8 x
g 9 i
b 10 d
a 10 c
h 12 j

所以要么你的测试数据是错误的,要么你必须做一些额外的错误处理(文件无法打开等)。

顺便说一句,你的这部分代码

                            else if (firstSpace != -1 && secondSpace == -1){

可以减少到

                            else {

因为你有一个 break 语句并在它之后立即将 secondSpace 设置回 -1。

编辑:您的数据也工作正常 - 输出是这样的:

Jone 75 jone@hotmail.com
Alice 78 alice@myclass.com
Nick 79 nick@bmail.com
Sean 80 sean@csi.edu
Mark 86 mark@gmail.com
Thomas 88 tom@cix.csi.edu
Zach 89 zach@yahoo.com
Claire 89 claire@yahoo.com
Amy 95 amy@hotmail.com
Eve 97 eve@nytimes.com
Issac 99 issac@mail.csi.edu
James 100 james@yahoo.com

关于c++ - 为什么此代码不输出到文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1586584/

相关文章:

javascript - 我不知道如何将 document.write 的输入存储在变量中

javascript - 单击下拉列表中的复选框时如何添加输入字段

c++ - 为什么使用 std::forward 禁用模板参数推导?

c++ - "const int& jj"和 "int& const jj"有什么区别?

c++ - 堆分配变量的结构成员对齐

javascript - 获取输入="number"时的ENTER点击事件

php - 保护 PHP session 免受 XSS 攻击

arrays - 大小来自用户输入的数组

c++ - 如何在 QTapAndHoldGesture 之后忽略 QTapGesture

c++ - 如何仅包含一次文件/防止 C++ 程序中旧 C API 中的函数重新定义