简体   繁体   中英

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 . This is necessary so that it can hold EOF , as this is not a valid char value. So you need to declare date as 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). 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] . I suspect you want to copy each character you read into the next element of text , not into all of them.

Finally, when you were saving into text , you didn't save the lowercase version.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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