输出时每隔一行都为空

时间:2021-06-09 01:06:00

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

我有一个问题,使用此代码将输出中的每隔一行都清空。所需的输出是:http://paste.ubuntu.com/1354365/

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

我收到的时候: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);

}

2 个解决方案

#1


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..

看起来你正在从左边读-2个字符,所以“打印在另一个上面”等等。

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;  
}

#2


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?

1)为什么有必要使用额外的outputBuffer步骤?

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

2)您知道strncpy()不保证以null方式终止它复制的字符串。

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.

3)更重要的是,output [i]尚未初始化,因此strncat()将在任何垃圾已经存在之后连接字符串。如果在创建每个输出[i]时使用calloc()而不是malloc(),那可能会有所帮助。你的输出[i]变量甚至可能是你的额外换行符。

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.

4)即使初始化为空字符串,您也可以轻松地溢出输出[i],因为您循环12次并且最多写入11个字符。空终止符为11 * 12 + 1 =写入100字节数组的133字节。

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:

一般来说,除非这是一个需要使用malloc()的类赋值,否则我不明白为什么你不只是在程序开始时声明一次变量并在每个循环开始时将它们归零:

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.

并且,正如其他人所说,你分配了一堆内存而不是试图释放它。在循环外部分配一次,或者只是跳过分配步骤并直接声明它们。

#1


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..

看起来你正在从左边读-2个字符,所以“打印在另一个上面”等等。

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;  
}

#2


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?

1)为什么有必要使用额外的outputBuffer步骤?

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

2)您知道strncpy()不保证以null方式终止它复制的字符串。

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.

3)更重要的是,output [i]尚未初始化,因此strncat()将在任何垃圾已经存在之后连接字符串。如果在创建每个输出[i]时使用calloc()而不是malloc(),那可能会有所帮助。你的输出[i]变量甚至可能是你的额外换行符。

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.

4)即使初始化为空字符串,您也可以轻松地溢出输出[i],因为您循环12次并且最多写入11个字符。空终止符为11 * 12 + 1 =写入100字节数组的133字节。

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:

一般来说,除非这是一个需要使用malloc()的类赋值,否则我不明白为什么你不只是在程序开始时声明一次变量并在每个循环开始时将它们归零:

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.

并且,正如其他人所说,你分配了一堆内存而不是试图释放它。在循环外部分配一次,或者只是跳过分配步骤并直接声明它们。