C 查找并用空格替换数组中的制表符?

C 查找并用空格替换数组中的制表符?,c,arrays,C,Arrays,本练习的目的是创建一个可以读取文件内容并将其复制到新文件的程序。但还有一件事我不知道该怎么做: 我还应该用7个空格替换文件中的所有选项卡('\t')。 我如何解决后一个问题?多谢各位 我用这些代码行进行了尝试,但显然不起作用。它还显示以下警告: 警告C4047:“=”:“char”与“char[8]”的间接寻址级别不同 for(int i=0;i

本练习的目的是创建一个可以读取文件内容并将其复制到新文件的程序。但还有一件事我不知道该怎么做: 我还应该用7个空格替换文件中的所有选项卡('\t')。 我如何解决后一个问题?多谢各位

我用这些代码行进行了尝试,但显然不起作用。它还显示以下警告:

警告C4047:“=”:“char”与“char[8]”的间接寻址级别不同

for(int i=0;i
这是我的代码:(当然,如果可以通过某种方式进行改进,请毫不犹豫地告诉我。谢谢)

#包括
#包括
内部主(空){
char-buf[500];
文件*fp,*nuovo;
错误没有错误,错误1;
/*lettura e creazione文件*/
if(err=fopen_s(&fp,“text.txt”,“r”)){
printf(“错误”);
}否则{
printf(“文件出租”);
} 
if(err1=fopen_s(&nuovo,“nuovo.txt”,“w”)){
printf(“错误”);
}否则{
printf(“文件创建”);
}
INTX=fread_s(基本单位,基本单位,1500,基本单位);
/*这是丢失代码的地方*/
fwrite(&buf,1,x,nuovo);
fclose(fp);
fclose(新墨西哥州);
}

不能用包含多个字符的字符串替换一个字符

解决此问题的一种方法如下:您可以在执行输出的同时进行替换,而不必进行替换


循环检查
buf
中的字符,并检查
buf[i]
是否为
选项卡。如果不是
选项卡
,请调用
fputc(buf[i],nuovo)
。否则,调用
fputs(“,nuovo)

当您复制源中的所有字符并用空格替换制表符时,必须创建一个新的动态分配缓冲区:

#include <malloc.h>


const char *whitespace = "       ";
int whitespaceLen = strlen( whitespace );
int newBufSize = 0;
char *newBuf = NULL;
int newBufPos = 0;

for (int i = 0; i < x; i++)
{
    int isTab = buf[i] == '\t';               // test if next char in buf is tab
    int charSize = isTab ? whitespaceLen : 1; // size which is neede for next char in newBuf

    if ( newBufPos + charSize < newBufSize ) // test if newBuf is large enough
    {
        newBufSize += 500;
        char *temp = realloc( newBuf, newBufSize ); // enlarge newBuf
        if ( temp == NULL )
        {
            free( newBuf ); // out of memory, free newBuf an terminate
            newBuf = NULL;
            break;
        }
        newBuf = temp;
    }

    if ( isTab )
    {
         memcpy( newBuf+newBufPos, whitespace, whitespaceLen ); // copy whitespace blanks to newBuf
         newBufPos += whitespaceLen ;
    }
    else
         newBuf[ newBufPos++ ] = buf[i];  // copy char to newBuf
}
#包括
常量字符*空白=”;
int whitespaceLen=strlen(空白);
int newBufSize=0;
char*newBuf=NULL;
int-newBufPos=0;
对于(int i=0;i

注意你必须
免费(newBuf)的地方

buf[i]=”(内联代码中不显示7个空格…)您试图在1个字符中添加7个字符,我知道这是错误的,但我不知道如何做一种可能是读取或写入单个字符,检查字符是否为制表符,如果是,则用7个空格替换。
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char buf[500];
    FILE *fp, *nuovo;
    errno_t err, err1;

    /*lettura e creazione file*/
    if (err = fopen_s(&fp, "text.txt", "r")) {
        printf("Errore");
    } else {
        printf("File Letto");
    } 
    if (err1 = fopen_s(&nuovo, "nuovo.txt", "w")) {
        printf("Errore");
    } else {
        printf("File Creato");
    }

    int x = fread_s(buf, sizeof(buf), 1, 500, fp);
    /*THIS IS THE PLACE FOR THE MISSING CODE*/


    fwrite(&buf, 1, x, nuovo);
    fclose(fp);
    fclose(nuovo);
}
#include <malloc.h>


const char *whitespace = "       ";
int whitespaceLen = strlen( whitespace );
int newBufSize = 0;
char *newBuf = NULL;
int newBufPos = 0;

for (int i = 0; i < x; i++)
{
    int isTab = buf[i] == '\t';               // test if next char in buf is tab
    int charSize = isTab ? whitespaceLen : 1; // size which is neede for next char in newBuf

    if ( newBufPos + charSize < newBufSize ) // test if newBuf is large enough
    {
        newBufSize += 500;
        char *temp = realloc( newBuf, newBufSize ); // enlarge newBuf
        if ( temp == NULL )
        {
            free( newBuf ); // out of memory, free newBuf an terminate
            newBuf = NULL;
            break;
        }
        newBuf = temp;
    }

    if ( isTab )
    {
         memcpy( newBuf+newBufPos, whitespace, whitespaceLen ); // copy whitespace blanks to newBuf
         newBufPos += whitespaceLen ;
    }
    else
         newBuf[ newBufPos++ ] = buf[i];  // copy char to newBuf
}