0

I have problem with getNumber function, because my output_file contains zeros. And in my opinion it should not. I want my program to print all numbers and then add them up.

Here's the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define CHUNK 12

char *getNumber(FILE *infile);

int main(int argc, char *argv[])
{
    char *number, *pEnd;
    FILE *infile, *outfile;
    int newNumber, sum = 0;

    if(argc != 3)
    {
        printf("Missing argument!\n");
        exit(1);
    }

    infile = fopen(argv[1], "r");
    if(infile != NULL)
    {
        outfile = fopen(argv[2], "w");
        if(outfile == NULL)
        {
            printf("Error, cannot open the outfile!\n");
            abort();
        }
        else
        {
            while(!feof(infile))
            {
                number = getNumber(infile);

                if(number == NULL)
                {
                    free(number);
                    abort();
                }
                newNumber = strtol(number, &pEnd, 10);
                sum += newNumber;

                 if(!*pEnd)
                    printf("Converted successfully!\n");
                    else printf("Conversion error, non-convertible part: %s", pEnd);

                fprintf(outfile, "%d\n", newNumber);
                free(number);
            }
            fprintf(outfile, "\nSum: %d\n", sum);
        }
    }
    else
    {
        printf("Error, cannot open the infile!\n");
        abort();
    }

    fclose(infile);
    fclose(outfile);
    return 0;
}   

char *getNumber(FILE *infile)
{
    char *number, *number2;
    int length, cursor = 0, c;dwwd

    number = (char*)malloc(sizeof(char)*CHUNK);
    if(number == NULL)
    {
        printf("Error!\n");
        return NULL;
    }

    length = CHUNK;

    while(!isspace(c = getc(infile)) && !feof(infile))
    {
        if(isdigit(c))
        {
            number[cursor] = c;
            cursor++;

            if(cursor >= length)
            {
                length += CHUNK;
                number2 = (char*)realloc(number, cursor);
                if(number2 == NULL)
                {
                    free(number);
                    return NULL;
                }
                else number = number2;
            }
        }
    }
    number[cursor] = '\0';
    return number;
}

I would be really grateful for any help.

I am also sending two files, input_file and output_file: input_file output_file

4

3 回答 3

1

你在这里的条件:

while(!isspace(c = getc(infile)) && !feof(infile))

每次遇到空间时都会中断。之后,您将始终打印该号码。这意味着对于没有直接以数字开头的每个间隔(也用于文件末尾),您将在输出文件中打印一个额外的零。

无论您是否至少输入了一次,都可以添加一个标志。如果你没有 - 只是不要打印任何东西。

于 2013-09-08T09:44:21.073 回答
0

从 strtol c 库联机帮助页:

如果根本没有数字,strtol() 将 nptr 的原始值存储在 *endptr 中(并返回 0)

您总是分配给 newNumber 并且不检查 strtol 实际上没有返回转换后的数字而是 a 而是返回 0 的情况,因为它找不到数字。这就是为什么您的输出文件中包含所有零的原因。

于 2013-09-08T09:48:06.303 回答
0

您必须在 if(isdigit(c)) 中添加 else 语句以在先前找到数字后找到非数字字符时中断循环。

if(isdigit(c))
{
    // your existing code
}
else if (cursor != 0)
{
    break;
}

希望这可以帮助。

编辑:

只需更换

fprintf(outfile, "%d\n", newNumber);

if(0 != newNumber) fprintf(outfile, "%d\n", newNumber);
于 2013-09-08T09:58:46.247 回答