File format [modified]
-
Hi all. Im having trouble making this file read-out look nice. The code is this:
#include using namespace std; int main(){ char buffer[100]; ifstream file("file.dat",ios::end); if(file.fail()){ printf("File not found.\n"); } printf("File contents read the following:\n"); while(file >> buffer){ printf("\n%s ",buffer); } file.close(); system("pause"); return 0; }
The file "file.dat" reads "This is only a test" inside. But when i run the program it reads: File contents read the following: This is only a test ----------------------- So i figured maybe it was a problem with my way of printing the message in the console. So i switched the printf around from thisprintf("\n%s ",buffer);
To thisprintf("%s\n ",buffer);
To thisprintf("%s",buffer); // This putsallthewordstogetherlikeso
And still cant come up with a better looking format. Is there another way i can make it print the way its saved in the file? Thanx in advance! P.S. If i made the print like this: printf("%s ",buffer); It would print the first line in the file as its written in the file. But if there were two lines or three etc. It prints it like this "This is only a test This is only a test This is only a test" Which isnt what im looking to do. I'd like it look something like This is only a test This is only a test This is only a test -
Hi all. Im having trouble making this file read-out look nice. The code is this:
#include using namespace std; int main(){ char buffer[100]; ifstream file("file.dat",ios::end); if(file.fail()){ printf("File not found.\n"); } printf("File contents read the following:\n"); while(file >> buffer){ printf("\n%s ",buffer); } file.close(); system("pause"); return 0; }
The file "file.dat" reads "This is only a test" inside. But when i run the program it reads: File contents read the following: This is only a test ----------------------- So i figured maybe it was a problem with my way of printing the message in the console. So i switched the printf around from thisprintf("\n%s ",buffer);
To thisprintf("%s\n ",buffer);
To thisprintf("%s",buffer); // This putsallthewordstogetherlikeso
And still cant come up with a better looking format. Is there another way i can make it print the way its saved in the file? Thanx in advance! P.S. If i made the print like this: printf("%s ",buffer); It would print the first line in the file as its written in the file. But if there were two lines or three etc. It prints it like this "This is only a test This is only a test This is only a test" Which isnt what im looking to do. I'd like it look something like This is only a test This is only a test This is only a testI think you may find this link very useful www.dorota.ecs.fullerton.edu/classes/CS231/lect3-cs.ppt or Google on "ios:end"
John P.
-
I think you may find this link very useful www.dorota.ecs.fullerton.edu/classes/CS231/lect3-cs.ppt or Google on "ios:end"
John P.
jparken wrote:
Is http://www.dorota.ecs.fullerton.edu/ a valid domain?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Hi all. Im having trouble making this file read-out look nice. The code is this:
#include using namespace std; int main(){ char buffer[100]; ifstream file("file.dat",ios::end); if(file.fail()){ printf("File not found.\n"); } printf("File contents read the following:\n"); while(file >> buffer){ printf("\n%s ",buffer); } file.close(); system("pause"); return 0; }
The file "file.dat" reads "This is only a test" inside. But when i run the program it reads: File contents read the following: This is only a test ----------------------- So i figured maybe it was a problem with my way of printing the message in the console. So i switched the printf around from thisprintf("\n%s ",buffer);
To thisprintf("%s\n ",buffer);
To thisprintf("%s",buffer); // This putsallthewordstogetherlikeso
And still cant come up with a better looking format. Is there another way i can make it print the way its saved in the file? Thanx in advance! P.S. If i made the print like this: printf("%s ",buffer); It would print the first line in the file as its written in the file. But if there were two lines or three etc. It prints it like this "This is only a test This is only a test This is only a test" Which isnt what im looking to do. I'd like it look something like This is only a test This is only a test This is only a testdellthinker wrote:
Is there another way i can make it print the way its saved in the file?
Yes. Either read and print it on a line-by-line basis, or read each word into an array and print the contents of the array as you see fit.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Hi all. Im having trouble making this file read-out look nice. The code is this:
#include using namespace std; int main(){ char buffer[100]; ifstream file("file.dat",ios::end); if(file.fail()){ printf("File not found.\n"); } printf("File contents read the following:\n"); while(file >> buffer){ printf("\n%s ",buffer); } file.close(); system("pause"); return 0; }
The file "file.dat" reads "This is only a test" inside. But when i run the program it reads: File contents read the following: This is only a test ----------------------- So i figured maybe it was a problem with my way of printing the message in the console. So i switched the printf around from thisprintf("\n%s ",buffer);
To thisprintf("%s\n ",buffer);
To thisprintf("%s",buffer); // This putsallthewordstogetherlikeso
And still cant come up with a better looking format. Is there another way i can make it print the way its saved in the file? Thanx in advance! P.S. If i made the print like this: printf("%s ",buffer); It would print the first line in the file as its written in the file. But if there were two lines or three etc. It prints it like this "This is only a test This is only a test This is only a test" Which isnt what im looking to do. I'd like it look something like This is only a test This is only a test This is only a testOperator >> used with (i/o)fstream read one word from actual position in file to first space or end line. If you want read line you must use getline. You can read about it there: http://www.cplusplus.com/istream::getline :)
----------------------------------------------------------------- Surely without war there would be no loss Hence no mourning, no grief, no pain, no misery No sleepless nights missing the dead... Oh, no more No more war [Sleepless - Cradle of Filth]