FILE HANDLING

B

BHARAT

Hi I am a little bit new to C's advanced topics:
I need help in file handling.
I have Turbo C and I wrote a simple program to write data to file:

#include<stdio.h>

#include<dir.h>
#include<conio.h>
#include<stdlib.h>

// program to write your name to a file
void main()
{ FILE *pt;
char *b;
chdir("d:\test");
pt=fopen("bharat.text","w");
printf("ENter ur name ");
scanf(" %[^\n]",b);
fprintf(pt," %s",b);
fclose(pt);
getch();
}

When I executed the program, a new file("bharat.text")was created in
the BIN directory under the Turbo C directory. I wish that the file
should be created in "D:\TEST" folder.
Can any one tell me how to do that?
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

BHARAT said:
Hi I am a little bit new to C's advanced topics:
I need help in file handling.
I have Turbo C and I wrote a simple program to write data to file:

#include<stdio.h>

#include<dir.h>
#include<conio.h>
#include<stdlib.h>

// program to write your name to a file
void main()
{ FILE *pt;
char *b;
chdir("d:\test");
pt=fopen("bharat.text","w");
printf("ENter ur name ");
scanf(" %[^\n]",b);
fprintf(pt," %s",b);
fclose(pt);
getch();
}

When I executed the program, a new file("bharat.text")was created in
the BIN directory under the Turbo C directory. I wish that the file
should be created in "D:\TEST" folder.
Can any one tell me how to do that?

Change "d:\test" to "d:\\test" or "d:/test".

Bjørn
 
K

Keith Thompson

BHARAT said:
Hi I am a little bit new to C's advanced topics:
I need help in file handling.
I have Turbo C and I wrote a simple program to write data to file:

#include<stdio.h>

#include<dir.h>
#include<conio.h>
#include<stdlib.h>

// program to write your name to a file
void main()

Make that "int main(void)".
{ FILE *pt;
char *b;
chdir("d:\test");

The "\t" expands to a tab character. If you want a backslash
character in a string, you need to double it:

chdir("d:\\test");
pt=fopen("bharat.text","w");
printf("ENter ur name ");

Apart from the misspellings, stdout is typically line-buffered, so the
message may not appear before the scanf(). Add
fflush(stdout);
scanf(" %[^\n]",b);
fprintf(pt," %s",b);
fclose(pt);
getch();

getch() is a non-standard function.

You should have a "return 0;" here.
 
K

Kenny McCormack

When I executed the program, a new file("bharat.text")was created in
the BIN directory under the Turbo C directory. I wish that the file
should be created in "D:\TEST" folder.
Can any one tell me how to do that?
[/QUOTE]

You'll get the usual:

Not portable. Can't discuss it here. Blah, blah, blah.

but see below anyway...
Change "d:\test" to "d:\\test" or "d:/test".

Or, more to the point, get in the habit of checking the return value of
system/library calls.
 
J

Jack Klein

Hi I am a little bit new to C's advanced topics:
I need help in file handling.
I have Turbo C and I wrote a simple program to write data to file:

#include<stdio.h>

#include<dir.h>
#include<conio.h>
#include<stdlib.h>

// program to write your name to a file
void main()
{ FILE *pt;
char *b;
chdir("d:\test");
pt=fopen("bharat.text","w");
printf("ENter ur name ");
scanf(" %[^\n]",b);
fprintf(pt," %s",b);
fclose(pt);
getch();
}

When I executed the program, a new file("bharat.text")was created in
the BIN directory under the Turbo C directory. I wish that the file
should be created in "D:\TEST" folder.
Can any one tell me how to do that?

In addition to everything Keith pointed out, you have a real problem.
You have defined a pointer to char, b, but have not created any
characters for it to point to, nor have you initialized it to point
anywhere.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top