Click to See Complete Forum and Search --> : files revisited


Hyena
May 20th, 1999, 09:51 AM
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
Could it be that you are writing your word to word.dat and trying to read it from words.dat??

Rail Jon Rogut
May 20th, 1999, 04:15 PM
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
railro@earthlink.net
http://home.earthlink.net/~railro/

Saeed R
May 20th, 1999, 05:48 PM
#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