在C中每行右侧的第n个位置添加一个逗号

在C中每行右侧的第n个位置添加一个逗号,c,C,我有这样一个文件: cat file.txt 100000000 1000000 10000000 100,000,000 1,000,000 10,000,000 我想提出: cat result.txt 100000000 1000000 10000000 100,000,000 1,000,000 10,000,000 到目前为止,我已经: #include <stdio.h> #include <string.h> int main(){ FIL

我有这样一个文件:

cat file.txt

100000000
1000000
10000000
100,000,000
1,000,000
10,000,000
我想提出:
cat result.txt

100000000
1000000
10000000
100,000,000
1,000,000
10,000,000
到目前为止,我已经:

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

int main(){
    FILE *file_to_read = fopen("file.txt","r");
    FILE *file_to_write = fopen("result.txt","w");
        char str[60];
    while(fgetc(file_to_read) != EOF){
        if( fgets(str, 60, file_to_read) != NULL ){
            fputs(str,file_to_write);
        }
    }
    fclose(file_to_read);
    fclose(file_to_write);
    return (0);
}
#包括
#包括
int main(){
FILE*FILE_to_read=fopen(“FILE.txt”,“r”);
FILE*FILE_to_write=fopen(“result.txt”,“w”);
char-str[60];
while(fgetc(文件读取)!=EOF){
如果(fgets(str,60,文件读取)!=NULL){
FPUT(str,文件写入);
}
}
fclose(从文件到读取);
fclose(文件到写入);
返回(0);
}
是否应该使用strtok在每三个位置的右侧添加逗号?

strtok()用于根据选定的delimeter(例如“-”)拆分字符串。这里的情况并非如此

首先,您必须为每一行分配所需的空间,即:

len = sizeof(line)
new_line_size = len + len/3 +  (len % 3 == 0) ? 0 : 1
然后,您必须将第一个逗号放在以下位置:

len % 3 if (len % 3 != 0) 

然后每隔3位放一个逗号。

下面是我尝试的
commaize()
函数。这个函数“逗号”每个字符串,无论它们是否代表一个数字,它们是否已经有逗号。。。。例如:
“-100”
“-100”

#包括
无效命令(字符*限制dst,常量字符*限制src){
无符号srclen=strlen(src);
if(srclen<4){
而((*dst++=*src++)/*void*/;
//dst正确终止
}否则{
开关(srclen%3){
案例0:*dst++=*src++;//故障排除;复制3个字符
案例2:*dst++=*src++;//故障排除;复制2个字符
案例1:默认值:*dst++=*src++;break;//复制1个字符
}
做{
*dst++=',';//通信
*dst++=*src++;*dst++=*src++;*dst++=*src++;//复制3个字符
}而(*src);
*dst=0;//正确终止dst
}
}

这里有一个解决方案,它一次读取一个字符,并使用标准的输入/输出流

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

#define LEN(array) (sizeof (array) / sizeof (array)[0])

int main(void)
{
    int n, i, j, ch;
    char buf[256];

    n = 1;
    i = 0;
    ch = getchar();
    while (ch != EOF) {
        if ((ch >= '0') && (ch <= '9')) {
            /*copy character to buffer*/
            if (i < LEN(buf)) {
                buf[i] = ch;
                i++;
            } else {
                fprintf(stderr, "Integer number %d has too many digits, maximum is %d\n", n, LEN(buf));
                exit(1);
            }
        } else if (i > 0) {
            /*print number*/
            for (j = 0; j < i; j++) {
                if ((j > 0) && ((i - j) % 3 == 0)) {
                    putchar(',');
                }
                putchar(buf[j]);
            }
            putchar('\n');
            n++;
            i = 0;
        }
        ch = getchar();
    }
    return 0;
}
#包括
#包括
#定义LEN(数组)(sizeof(数组)/sizeof(数组)[0])
内部主(空)
{
int n,i,j,ch;
char-buf[256];
n=1;
i=0;
ch=getchar();
while(ch!=EOF){
如果((通道>='0')&(通道0){
/*打印号码*/
对于(j=0;j0)&((i-j)%3==0)){
putchar(',');
}
putchar(buf[j]);
}
putchar('\n');
n++;
i=0;
}
ch=getchar();
}
返回0;
}

假设输入行仅包含数字的简单实现(为简洁起见,省略了错误检查和输入验证):

#包括
#包括
内部主(空)
{
字符行[1000];
FILE*ifp=fopen(“FILE.txt”、“r”);
文件*ofp=fopen(“result.txt”,“w”);
while(fgets(line,sizeof line,ifp)!=NULL){
常量int len=strchr(行,'\n')-行;
fprintf(of p,'%s',(len-1)%3+1,行);
对于(inti=(len-1)%3+1;i
No,
strtok()
不是您正在寻找的函数。在循环条件中对
fgets
的调用将读取每行的第一个字符,然后将其删除。您将丢失每行的第一个字符。为什么不在循环条件中调用
fgets
?您可能只需要使用
%'d
printf格式说明符。Also、
“100”
变为
,100”
“fubar”
变为
“fu,bar”
?OT:您需要检查
fopen
是否成功