Click here to Skip to main content
15,917,652 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
#include<stdio.h>


void read()

{

int i=0 , j=0;
char s[10];

    FILE *fp ;

fp = fopen ( "sig.c" , "w+" );

    if (fp==NULL)
    {fputs("File error",stderr); exit (1);}


  fgets (s , 10 , fp) ;

 printf("%s \n",s);


fclose(fp);


}




void write()

{
char x1[10] ;

int i =0 ;
char c = "";

FILE *fp;
fp=fopen("sig.c", "wb");

for (i=0 ; i< 10 ;i++)

{

x1[i] = scanf("%c" , &c);
}

char x="f" ,
x2 = "";

for (i=0 ; i< 10 ;i++)

{

x2 = x1[i];

fwrite(&x2, sizeof(x2) , x ,fp);
}


fclose(fp);

}


void  main()

{
    write();

    read();


}



This project error when print s in read func. Why does the code not copy the entered char to sig.txt
Posted
Updated 6-May-12 7:15am
v2
Comments
enhzflep 6-May-12 11:28am    
It would probably help if the program was actually compiled and run. Failing to do so will certainly ensure that your program doesn't put new data into sig.txt

Have a look at the definition for c - a char. You certainly can't assign a string to a char, as in c = "f". Can't really tell if you've (somehow?) lost the * from the declaration, if it's someone else's code or what.

1 solution

Open the file in your read function with mode "r" instead of "w+". The mode "w+" will delete the contents of your file if it exists. That is not what you want.

x2 is nowhere defined in your write function, so I assume you pasted a simplified copy, which unfortunately does not compile.

I would recommend working with proper indenting, if not for yourself, then at least for others who read your programs.
 
Share this answer
 
v2

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