2

I have a problem with getting every other line empty on output with this code. The desired output is: http://paste.ubuntu.com/1354365/

While I get: http://paste.ubuntu.com/1356669/

Does anyone have an idea of why I'm getting these empty lines on every other line?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *fp; 
FILE *fw; 


int main(int argc, char *argv[]){
char buffer[100];
char *fileName = malloc(10*sizeof(char));
char **output = calloc(10, sizeof(char*));
char **outputBuffer = calloc(10, sizeof(char*));

fw = fopen("calvin.txt", "w+");

for(int y = 0; y < 6; y++){
    for(int i = 0; i < 10; i ++)
    {
        output[i] = malloc(100);
    }

    for(int x = 0; x < 12; x++){
        sprintf(fileName,"part_%02d-%02d", x, y);
        fp = fopen(fileName, "rb");

        if(fp == NULL)
        {
            printf("Kan ikke åpne den filen(finnes ikke/rettigheter)\n");
        }
        else if(fp != NULL){

            memset(buffer, 0, 100); 
            for(int i = 0; i < 10; i++){ 
                outputBuffer[i] = malloc(100);
            }


            fread(buffer, 1, 100, fp); 


            for(int i = 0; i < 100; i++){
                if(buffer[i] == '\0')
                {
                    buffer[i] = ' ';
                }
                else if(buffer[i] == '\n')
                {
                    buffer[i] = ' ';
                }
            }
            for(int i = 0; i < 10; i++) { 
                strncpy(outputBuffer[i], buffer + i * 10, 10);

                strncat(output[i], outputBuffer[i]+1, 11);


            }                       
        }

    }

    for(int i = 0; i < 10; i++){
        printf("%s\n", output[i]);

    }

}
fclose(fp);
free(fileName);

}
4

2 回答 2

0

You are not reading correcting from the file. On the first image in the beginning you have:

      o ""oo    " o o o

on the second

      ""oo     o o o

That does not make a lot of sense because it is the first line. It is not related to empty lines since we are talking about the first line.

It seems that you are reading -2 characters from the left so " prints over o the other " on the ' ' ect..

Try this away, may not be the most efficient solution:

int read(char *file)
{
     FILE *fp = NULL;         
     int size = 0, pos = 0,i;
     fp = fopen(file,"r");
     if (!fp) return 0;

     for(; ((getc(fp))!=EOF);  size++); // Count the number of elements in the file

     fclose(fp);


     char buffer[size];

     fp = fopen(file,"r");

     if (!fp) return 0;

     while((buffer[pos++]=getc(fp))!=EOF); // Saving the chars into the buffer

     for(i = 0; i < pos; i++)              // print them.
       printf("%c",buffer[i]);

    fclose(fp);
    return 1;  
}
于 2012-11-13T22:50:36.973 回答
0

This part seems problematic:

           strncpy(outputBuffer[i], buffer + i * 10, 10);

           strncat(output[i], outputBuffer[i]+1, 11);

1) Why is it necessary to use the extra outputBuffer step?

2) You know that strncpy() isn't guaranteed to null-terminate the string it copies.

3) More significantly, output[i] hasn't been initialized, so strncat() will concatenate the string after whatever junk is already in there. If you use calloc() instead of malloc() when creating each output[i], that might help. It's even possible that your output[i] variables are what hold your extra newline.

4) Even if initialized to an empty string, you could easily overflow output[i], since you're looping 12 times and writing up to 11 characters to it. 11 * 12 + 1 for the null terminator = 133 bytes written to a 100-byte array.

In general, unless this is a class assignment that requires use of malloc(), I don't understand why you aren't just declaring your variables once, at the start of the program and zeroing them out at the start of each loop:

char fileName[10];
char output[10][100];
char outputBuffer[10][100];

And, as stated by others, your allocating a bunch of memory and not trying to free it up. Allocate it once outside of your loop or just skip the allocation step and declare them directly.

于 2012-11-14T00:52:10.447 回答