简体   繁体   English

在c中读取和写入文件

[英]Reading and writing into a file in c

I need to write into a file with uppercase some strings ,then to display on screen with lowercase.我需要用大写的一些字符串写入文件,然后用小写在屏幕上显示。 After that ,I need to write into file the new text (lowercase one).之后,我需要将新文本(小写字母)写入文件。 I write some code ,but it doesn't work.我写了一些代码,但它不起作用。 When I run it , my file seems to be intact and the convert to lowercase don't work当我运行它时,我的文件似乎完好无损并且转换为小写不起作用

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

void main(void) {
    int i;
    char date;
    char text[100];
    FILE *file;
    FILE *file1;
    file = fopen("C:\\Users\\amzar\\Desktop\\PC\\Pregatire PC\\Pregatire PC\\file\\da.txt","r");
    file1 = fopen("C:\\Users\\amzar\\Desktop\\PC\\Pregatire PC\\Pregatire PC\\file\\da.txt","w");

    printf("\nSe citeste fisierul si se copiaza textul:\n ");

    if(file) {
        while ((date = getc(file)) != EOF) {
            putchar(tolower(date));
            for (i=0;i<27;i++) {
                strcpy(text[i],date);
            }
        }    
    }

    if (file1) {
        for (i=0;i<27;i++)
        fprintf(file1,"%c",text[i]);
    }
}

There are several problems with your program.你的程序有几个问题。

First, getc() returns int , not char .首先, getc()返回int ,而不是char This is necessary so that it can hold EOF , as this is not a valid char value.这是必要的,以便它可以保存EOF ,因为这不是有效的char值。 So you need to declare date as int .所以你需要将date声明为int

When you fix this, you'll notice that the program ends immediately, because of the second problem.当你解决这个问题时,你会注意到程序立即结束,因为第二个问题。 This is because you're using the same file for input and output.这是因为您使用相同的文件进行输入和输出。 When you open the file in write mode, that empties the file, so there's nothing to read.当您以写入模式打开文件时,会清空文件,因此没有任何内容可读取。 You should wait until after you finish reading the file before you open it for output.您应该等到读完文件后再打开它进行输出。

The third problem is this line:第三个问题是这一行:

strcpy(text[i],date);

The arguments to strcpy() must be strings, ie pointers to null-terminated arrays of char , but text[i] and date are char (single characters). strcpy()的参数必须是字符串,即指向以空char结尾的char数组的指针,但text[i]datechar (单个字符)。 Make sure you have compiler warnings enabled -- that line should have warned you about the incorrect argument types.确保您启用了编译器警告——该行应该警告您有关不正确的参数类型。 To copy single characters, just use ordinary assignment:要复制单个字符,只需使用普通赋值:

text[i] = date;

But I'm not really sure what you intend with that loop that copies date into every text[i] .但我不太确定你打算用那个将date复制到每个text[i]循环。 I suspect you want to copy each character you read into the next element of text , not into all of them.我怀疑您想将您读到的每个字符复制到text的下一个元素中,而不是复制到所有字符中。

Finally, when you were saving into text , you didn't save the lowercase version.最后,当您保存到text ,您没有保存小写版本。

Here's a corrected program.这是一个更正的程序。 I've also added a null terminator to text , and changed the second loop to check for that, instead of hard-coding the length 27.我还在text添加了一个空终止符,并更改了第二个循环来检查它,而不是硬编码长度 27。

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

void main(void) {
    int i = 0;
    int date;
    char text[100];
    FILE *file;
    FILE *file1;
    file = fopen("C:\\Users\\amzar\\Desktop\\PC\\Pregatire PC\\Pregatire PC\\file\\da.txt","r");

    printf("\nSe citeste fisierul si se copiaza textul:\n ");

    if(file) {
        while ((date = getc(file)) != EOF) {
            putchar(tolower(date));
            text[i++] = tolower(date);
        }
        text[i] = '\0'; 
        fclose(file);
    } else {
        printf("Can't open input file\n");
        exit(1);
    }

    file1 = fopen("C:\\Users\\amzar\\Desktop\\PC\\Pregatire PC\\Pregatire PC\\file\\da.txt","w");
    if (file1) {
        for (i=0;text[i] != '\0';i++)
            fprintf(file1,"%c",text[i]);
        fclose(file1);

    } else {
        printf("Can't open output file\n");
        exit(1);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM