Click here to Skip to main content
15,910,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
<pre>#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void create();
void read();
void edit();
void main()
{
    system("clear");
    int n,a,i;
    printf("enter your choise\n1:create file\n2:read file\n3:edit file\nyour answer : ");
    scanf("%d",&n);
    printf("enter how many file do you need\nyour answer : ");
    scanf("%d",&a);
    if(n==1)
    {
        for(i=1;i<=a;i++)
        {
            create();
            system("clear");
        }
    }
    else if(n==2)
    {
        for(i=1;i<=a;i++)
        {
            read();
        }
    }
    else
    {
        for(i=1;i<=a;i++)
        {
            edit();
            system("clear");
        }
    }
}
void create()
{
    char file[]="",data[1000];
    printf("enter your file name and add '.txt' file formate\nyour answer : ");
    scanf("%s",file);
    FILE *joker1=fopen(file,"w");
    printf("enter here\n");
    fgetc(stdin);
    fgets(data,1000,stdin);
    fprintf(joker1,"%s",data);
    fclose(joker1);
}
void read()
{
    char file[]="",data[1000];
    printf("enter your file name and add '.txt' in the end file formate\nyour answer : ");
    scanf("%s",file);
    FILE *joker2=fopen(file,"r");
    while(!feof(joker2))
    {
        fgets(data,1000,joker2);
        printf("%s",data);
    }
     
fclose(joker2);
}
void edit()
{
    char file[]="",data[1000];
    printf("enter your file name and add'.txt' file formate\nyour answer : ");
    scanf("%s",file);
    FILE *joker3=fopen(file,"a");
    printf("enter here\n");
    fgetc(stdin);
    fgets(data,1000,stdin);
    fprintf(joker3,"%s",data);
    fclose(joker3);
}


What I have tried:

i have tried lot of method but it didn't work can anyone please help me
in read mode the data in file is getting printed 2 time in main program.but if you separate the read function then it works perfect . Idon't know what is problem in this code
Posted
Updated 22-May-21 5:47am

Your problem is here:
C++
while(!feof(joker2))
{
    fgets(data,1000,joker2);
    printf("%s",data);
}
feof() returns true only after an EOF has been read. What is happening in your case is that fgets() reads up to and including the last '\n' in the file. At this point feof() is still false, since no EOF has been seen. At the next call to fgets(), the EOF condition is set, and data is unchanged. You then print data again, and then end the loop because now feof() is true. To solve this use the following:
C++
while(true)
{
    fgets(data, 1000, joker2);
    if(feof(joker2)
        break;
    printf("%s", data);
}
 
Share this answer
 
Comments
JÓKÊR KìÑG 22-May-21 12:16pm    
it prints only one line of text
Another problem you have
C++
char file[]=""; // here you create a char array of size 1
char data[1000];
printf("enter your file name and add '.txt' file formate\nyour answer : ");
scanf("%s",file); // and here you store a file name which is longer

Contrary to strings, char arrays are dynamically resized at runtime.
Char arrays are used to store zero terminated strings
A correction can be to force the size in first:
C++
char file[100]=""; // here, you have a problem only if file name is more than 99 chars
char data[1000];
printf("enter your file name and add '.txt' file formate\nyour answer : ");
scanf("%s",file);
 
Share this answer
 
v2
Comments
JÓKÊR KìÑG 22-May-21 12:15pm    
it didn't work
Patrice T 22-May-21 12:21pm    
Can you elaborate ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900