c - 在C中将字符串写入文本文件(文件开头有额外的空格)

标签 c string file

我在将字符串写入文本文件并从文件中读取时遇到问题。

输入字符串 (char text1) 正确写入文件 (input.txt) 并读取。 但是我的结果文件有问题 - 字符串似乎正确写入文件,但如果我查看文件,文件开头的结果字符串之前有一个空格。如果我输入文本“天气是天气”,那么在结果文件中我有这个 - “天气就是天气”。结果字符串文本没问题,唯一的问题是由于某种原因,结果文件的开头有一个空格。

当我使用此代码在屏幕上打印结果文件的内容时

while((ch2 = fgetc(result)) != EOF)
        printf("%c", ch2);

它什么也不打印,但是如果我用 puts(text2); 打印 text2 字符串本身(不是来自文件),那么它会正确打印。

导致此问题的原因是什么以及如何解决?

这是整个程序:

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


int main()
{
    char text1[200], text2[200], words[20][100], *dist, ch1, ch2;
    int i, j, nwords=0;

    FILE *input, *result;

    input = fopen("input.txt", "w");

    if(input == NULL)
    {
        perror("Error opening the file.\n");
        exit(EXIT_FAILURE);         
    }

// Text input

    printf("\n Enter the text:\n\n   ");
    gets(text1);
    fputs(text1, input);
    fclose(input);


// Split string into words

    dist = strtok(text1, " ,.!?");
    i=0;
    while(dist!=0)
    {      
        strcpy(words[i],dist);
        dist = strtok(NULL, " ,.!?");
        i++;
        nwords++;                
    }


// Duplicating words that doesn't repeat in input string and copy them into tex2 string

    int flag_arr[20];
    memset(flag_arr, 0, 20);

    for(i=0; i <= nwords-1; i++)
    {
        for(j=0; j<=nwords-1; j++)
        {
            if(strcmp(words[i],words[j])==0)
            {
                flag_arr[i] += 1;
            }
        }
    }

    for(i = 0; i <=nwords-1; i++)
    {
        if(flag_arr[i] > 1)
        {
            strcat(text2," ");
            strcat(text2,words[i]);                              
        }
        else
        {
            strcat(text2," ");
            strcat(text2,words[i]);
            strcat(text2," ");
            strcat(text2,words[i]);
        }
    }

    result = fopen("result.txt", "w");

    if(result == NULL)
    {
        perror("Error opening the file.\n");
        exit(EXIT_FAILURE);         
    }

    fputs(text2, result);
    fclose(result);


// Rezultats

    fopen("input.txt", "r");
    if(input == NULL)
    {
        perror("Error opening the file.\n");
        exit(EXIT_FAILURE);         
    }
    fopen("result.txt", "r");
    if(result == NULL)
    {
        perror("Error opening the file.\n");
        exit(EXIT_FAILURE);         
    }

    printf("\n\n\n Input:\n\n   ");
    while((ch1 = fgetc(input)) != EOF)
        printf("%c", ch1);
//    puts(input);

    printf("\n\n\n Result:\n\n  ");
    while((ch2 = fgetc(result)) != EOF)
        printf("%c", ch2);   
//    puts(text2);

    fclose(input);
    fclose(result);

    getchar();
    return 0;
}

最佳答案

这是因为 stdout缓冲的。要么在打印中添加换行符,要么使用显式刷新缓冲区

fflush(stdout);
<小时/>

你有这条线

input = fopen("input.txt", "w");

还有这一行(两次,一次就够了)

fopen("input.txt", "r");

您实际上没有在任何地方重新分配input变量。这意味着当您尝试从 input 读取数据时,您将在尝试从只写文件句柄读取数据时收到错误(您不会报告该错误)。

关于c - 在C中将字符串写入文本文件(文件开头有额外的空格),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19769386/

相关文章:

c - 使用 C 中的静态库重定向标准输出(例如到文件)

C为什么这个for循环在满足条件时不执行?

c - 如何知道一个函数是否在 C 中被调用,是否使用 gdb 或任何其他工具进行类型转换?

string - Ada 中的多行字符串文字

c# - C# 中的自然排序顺序

c++ - 为什么 'allocate in one library and free in the other'是错误的

php - 如何在doctrine2实体中禁用字符串最大长度的隐式字符串修剪?

c - 从字符串中删除括号中的文本?

c++ - std::fstream 缓冲与手动缓冲(为什么手动缓冲有 10 倍增益)?

java - 使用java从文件中删除一行