all0star
all0star

Reputation: 99

How to remove leading zeros from output_file?

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

Upvotes: 0

Views: 161

Answers (3)

Vinod Kumar Y S
Vinod Kumar Y S

Reputation: 628

You have to add else statement to your if(isdigit(c)) to break the loop when a non digit character is found after having found a digit previously.

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

Hope this helps.

EDIT:

Just replace

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

with

if(0 != newNumber) fprintf(outfile, "%d\n", newNumber);

Upvotes: 0

JeSuisse
JeSuisse

Reputation: 359

From the strtol c library manpage:

If there were no digits at all, strtol() stores the original value of nptr in *endptr (and returns 0)

You always assign to newNumber and don't check for the case where strtol doesn't actually return a converted number but a instead returns a 0 because it couldn't find a number. That's why you have all the zeroes in your output file.

Upvotes: 0

Boris Strandjev
Boris Strandjev

Reputation: 46943

Your condition here:

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

Breaks every time you encounter space. After that you will always print the number. That means that for every interval(also for the end of the file) that is not preceded directly with digit you will print one extra zero in the output file.

Maybe add one flag whether you entered the while at least once. If you have not - just do not print anything.

Upvotes: 1

Related Questions