为什么这段代码不能输出到文件?

时间:2009-10-19 01:26:44

标签: 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;
}

这是我的数据:
Sean 80 sean@csi.edu
James 100 james@yahoo.com
Issac 99 issac@mail.csi.edu
Thomas 88 tom@cix.csi.edu
Alice 78 alice@myclass.com
Jone 75 jone@hotmail.com
扎克89 zach@yahoo.com
Mark 86 mark@gmail.com
尼克79 nick@bmail.com
Amy 95 amy@hotmail.com
Claire 89 claire@yahoo.com
Eve 97 eve@nytimes.com

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

printLowestRecord("gradebook.txt");
sortFileRecords("gradebook.txt", "sortedGradebook.txt");

也许您需要指定文件的完整绝对路径?

答案 2 :(得分:0)

printLowestRecord("gradebook.txt");
sortFileRecords("gradebook.txt", "sortedGradebook.txt");

在这两行中,尝试为输出文本编写完整文件路径。我在想这就是问题所在。