|
-
May 20th, 1999, 09:51 AM
#1
files revisited
i'm just beginning files, and am in need of help. program makeword (below) simply saves an inputted word to a file. readword should read this file and print out the word, but it doesn't work. help. i already posted this once, and added the suggested changes, but it didn't make any difference. help.
//program makeword- makes a word and stores it in a file
#include <stdio.h>
#include <iostream.h>
void main()
{
FILE *fPtr;
fPtr = fopen("a:\\word.dat", "w");
char *word = new char[30];
cout << "Enter a word: ";
scanf("%s",word);
fprintf(fPtr, "%s", word);
fclose(fPtr);
}
//============================================================//
//program readword- reads data from makeword
#include <iostream.h>
#include <stdio.h>
void main()
{
FILE *fPtr;
char *word = new char[30];
if((fPtr = fopen("a:\\words.dat","r")) != NULL)
{
fscanf(fPtr,"%s", word);
printf("%s",word);
fclose(fPtr);
}
else { cout << "could not be opened."; }
}
-
May 20th, 1999, 01:41 PM
#2
Re: files revisited
Could it be that you are writing your word to word.dat and trying to read it from words.dat??
-
May 20th, 1999, 04:15 PM
#3
Re: files revisited
Besides the fact (as mentioned earlier) that the two files are named differently, your program will have memory leaks as shown -- remember to delete any memory allocated with new before the pointer variable goes out of scope.
Rail
Recording Engineer/Software Developer
Rail Jon Rogut Software
[email protected]
http://home.earthlink.net/~railro/
-
May 20th, 1999, 05:48 PM
#4
Re: files revisited
#include <stdio.h>
#include <iostream.h>
void main()
{
char *szFile="dummy.dat";
FILE *fPtr;
fPtr = fopen(szFile, "w");
char *word = new char[30];
cout << "Enter a word: ";
scanf("%s",word);
fprintf(fPtr, "%s", word);
fclose(fPtr);
if((fPtr = fopen(szFile,"r")) != NULL)
{
fscanf(fPtr,"%s", word);
printf("\nREAD STUFF WAS:%s",word);
fclose(fPtr);
}
else
{
cout << "could not be opened.";
}
delete word;
}
I hust had to make sure the problem had to be that simple and it was
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|