#include <stdio.h>
|
|
int
fputs (const char *str, FILE *stream); |
|
int
puts (const char *str); |
The function puts writes the string str, and a terminating newline character, to the stream stdout.
/****************** this program shows writing characters from a file using fputs **************/
/****************** consider input.txt has the following content: ******************************/
/****************** hello world ****************************************************************/
#include <stdio.h>
int main(void)
{
int wretval;
char rs1[50],rs2[50];
char *rptr;
int retval;
FILE* fp = fopen("c:\nput.txt", "w");
fprintf(fp, "%s", "abcdefghijklmn");
fprintf(fp, "%c", "\n");
fprintf(fp, "%s", "fdsfdsafsdabcdefghijklmn");
fclose(fp);
fp = fopen("c:\nput.txt","r");
if(fp == NULL)
{
printf("fopen failed\n");
return -1;
}
rptr = fgets(rs1,12,fp);
if(rptr == NULL)
{
printf("fgets failed\n");
fclose(fp);
return -1;
}
fclose(fp);
fp = fopen("c:\uts1.txt","w+");
if(fp == NULL)
{
printf("fopen failed\n");
return -1;
}
wretval = fputs(rs1,fp);
if(wretval == EOF)
{
printf("fputs failed\n");
fclose(fp);
return -1;
}
fclose(fp);
fp = fopen("C:\uts1.txt","r");
if(fp == NULL)
{
printf("fopen failed\n");
return -1;
}
rptr = fgets(rs2,12,fp);
if(rptr == NULL)
{
printf("fgets failed\n");
fclose(fp);
return -1;
}
printf("file reading returned \"%s\"\n",rs2);
fclose(fp);
unlink("C:\uts1.txt");
return 0;
}
Output
file reading returned "abcdefghijk"
| [EBADF] | |
| The stream argument is not a writable stream. | |
The functions fputs and puts may also fail and set errno for any of the errors specified for the routines write.
|
© 2005-2007 Nokia |