Problem in file handling in C

Why am I getting no value in ch?
1
2
3
4
5
6
7
8
9
10
11
 #include<stdio.h>
int main()
{
	FILE *f;
	char ch;
	f=fopen("D:\Hello.prat","w+");
	ch=getchar();
	putc(ch,f);
	ch=getc(f);
	printf("%c",ch);
}
The '\' character needs to be doubled. the backslash is used for special characters such as '\n' = newline or '\t' = tab, a plain backslash is represented as '\\'.


After writing to the file, use rewind() to reset the position to the start.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

int main()
{
	FILE *f;
	char ch;
	f=fopen("D:\\Hello.prat","w+");
	ch=getchar();
	putc(ch,f);
	rewind(f);  // reset position to start
	ch=getc(f);
	printf("%c",ch);
}

http://www.cplusplus.com/reference/cstdio/rewind/
Topic archived. No new replies allowed.