与.txt文件之间的输入输出问题

时间:2019-02-10 23:11:28

标签: c

我的作业是计算电阻的电阻(以欧姆为单位),并给出一个文本文件,其中包含有关电阻的信息。使用此文本文件,我可以计算电阻并将其打印到另一个文本文件中。运行此代码时,出现调试错误“运行时检查失败#3-T”,并且没有输出到文件。你能告诉我我要去哪里了吗?

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<math.h>
int col_to_num(char color, int choice) {
    int num;
    switch (color) {
    case 'B':if (choice == 1) num = 0;
             else if (choice == 2) num = 1;
             else if (choice == 3)num = 6;

    case 'b':if (choice == 1) num = 0;
             else if (choice == 2) num = 1;
             else if (choice == 3)num = 6;

    case 'G':if (choice == 1) num = 5;
             else if (choice == 2) num = 8;
             else if (choice == 3)num = -1;
    case 'g':if (choice == 1) num = 5;
             else if (choice == 2) num = 8;
             else if (choice == 3)num = -1;
    case 'R':num = 2;
    case 'r':num = 2;
    case 'O':num = 3;
    case 'o':num = 3;
    case 'Y':num = 4;
    case 'y':num = 4;
    case 'V':num = 7;
    case 'v':num = 7;
    case 'W':num = 9;
    case 'w':num = 9;
    case 'S':num = 10;
    case 's':num = 10;
    }
    return num;
}
int main() {
    double resistance, thirdband;
    char color1, color2, color3;
    int val1, val2, val3;
    FILE *inp, *outp;
    inp = fopen("resistorcolor.txt", "r");
    outp = fopen("resistorvalue.txt", "w");
    if (inp == NULL) {
        printf("The input file does not exist\n");
    }
    else {
        while (fscanf(inp, "%c %d %c %d %c %d%*c", &color1, &val1, &color2, &val2, &color3, &val3) != EOF) {
            if (col_to_num(color3, val3) == -1)thirdband = .01;
            if (col_to_num(color3, val3) == 10)thirdband = .01;
            if (col_to_num(color3, val3) == 0)thirdband = 1;
            if (col_to_num(color3, val3) == 1)thirdband = 10;
            if (col_to_num(color3, val3) == 2)thirdband = 100;
            if (col_to_num(color3, val3) == 3)thirdband = 1000;
            if (col_to_num(color3, val3) == 4)thirdband = 10000;
            if (col_to_num(color3, val3) == 5)thirdband = 100000;
            if (col_to_num(color3, val3) == 6)thirdband = 1000000;
            resistance = ((col_to_num(color1, val1) * 10) + col_to_num(color2, val2))*thirdband;
            fprintf(outp, "%.2lf Ohm", resistance);
            thirdband = 0;
        }
        fclose(outp);
    }
    return 0;
}

2 个答案:

答案 0 :(得分:3)

运行时检查失败#3表示使用了某些内容而未初始化。您没有看到更多信息。错误消息应该是“运行时检查失败#3:变量'XXX'正在使用而未初始化”,其中XXX是代码中的变量。

无论如何,如果您有很多未初始化的变量,则应将其初始化为一些默认值:

double resistance = 0.0, thirdband = 0.0;
char color1 = 0, color2 = 0, color3 = 0;
int val1 = 0, val2 = 0, val3 = 0;
FILE *inp = nullptr, *outp = nullptr;

以及功能:

int col_to_num(char color, int choice) {
    int num = -1;

最有可能是col_to_num函数中的int num,如果您没有传递这些值之一,它将永远不会成功。您通常需要在条件满足后才能中断以退出switch语句,但是您可以通过不中断来将案例组合在一起,从而使您可以轻松地对不同的案例执行相同的操作。可以认为它是从匹配的案例开始的。如果没有匹配的大小写,您还需要一个默认的大小写来处理:

int col_to_num(char color, int choice) {
    int num = -1;
    switch (color) {
    case 'B':
    case 'b':
        if (choice == 1) num = 0;
        else if (choice == 2) num = 1;
        else if (choice == 3)num = 6;
        break;
    case 'G':
    case 'g':
        if (choice == 1) num = 5;
        else if (choice == 2) num = 8;
        else if (choice == 3)num = -1;
        break;
    case 'R':
    case 'r':
        num = 2;
        break;
    case 'O':
    case 'o':
        num = 3;
        break;
    case 'Y':
    case 'y':
        num = 4;
        break;
    case 'V':
    case 'v':
        num = 7;
        break;
    case 'W':
    case 'w':
        num = 9;
        break;
    case 'S':
    case 's':
        num = 10;
        break;
    default:
        printf("Invalid input: %c\n", color);
    }
    return num;
}

答案 1 :(得分:0)

也许您对开关盒的理解不正确。您需要在每个案例之后添加break语句(如果未添加),无论输入如何,所有案例都将运行。第二件事是,必须在使用变量之前对其进行初始化。希望能有所帮助。干杯!随时提出任何疑问。