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


Hyena
May 24th, 1999, 12:07 PM
heres my problem- this should create a sequenctial file (first program) and then read it (second program). but it doesn't work- instead, when reading the inputted words, it just concatinates them. additionally, it makes a copy of the last word.


#include <stdio.h>
#include <iostream.h>

void main()
{
FILE *fPtr;

char *word = new char[30];

if ((fPtr = fopen("a:\\word.dat", "w")) == NULL)
printf("File could not be opened\n");
else {



cout << "Enter a word: ";
scanf("%s",word);
fprintf(fPtr, "%s", word);


while(!feof(stdin)) { //hit ctrl + z to stop
cout << "Enter a word: ";
scanf("%s",word);
fprintf(fPtr, "%s", word);

}

fclose(fPtr);
}

delete word;
}



//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

#include <iostream.h>
#include <stdio.h>

void main()
{
FILE *fPtr;
char *word = new char[30];

if((fPtr = fopen("a:\\word.dat","r")) == NULL)
printf("File could not be opened\n");
else {

while(!feof(fPtr)) {
fscanf(fPtr,"%s", word);
printf("%s",word);
cout << "\n";

}


fclose(fPtr);
}

delete word;
}

Wayne Fuller
May 24th, 1999, 12:38 PM
When you write out the file you need to insert your own carriage return ('\n')

As in
fprintf(fPtr, "%s\n", word);



And in the last function you are using both cout and printf. Both are printing to stdout.

Wayne