Archived

This topic is now archived and is closed to further replies.

Filip

c++ write

Recommended Posts

my problem is simple.I need to write (c++ language) in a file(from a string most likely) but line by line so that when i shoot out a line it puts newline on the end and jumps on the next line,newline process repeats itself...After i need to read it in the same way, after reding a line(in a string most likely) and when encountering newline it jumps to the next line...repeating until it has nothing to read!CODE PLEASE!(expl are welcome!)PS:i was wondering if i may hide and encrypt a file with sensitive data, so that only program is able to modify it?THANKS!

Share this post


Link to post
Share on other sites
This looks like the kind of homework assignment people get in their first programming class. We won''t do your homework for you.

Any decent book on C++ programming will give you the information you need to read and write files.

Look at the classes ifstream and ofstream.

Yes, you can encrypt files. However, that doesn''t necessarily mean only your program can modify it, as encryptions can be broken and keys can be stolen...

Share this post


Link to post
Share on other sites
here is a simple way to do what you''re looking for, and nothing more.

note: this may or may not compile.

#include <stdio.h>
char *filename = "file.txt"
char buffer[100];
FILE *file = fopen(filename, "r");
fgets(file, buffer, 100); // read line until ''\n''
// put this in a loop to read until end-of-file.
// which it returns NULL i believe.
// then simply close the file.
fclose(file);

hope that helps.

Share this post


Link to post
Share on other sites
quote:
Original post by GekkoCube
here is a simple way to do what you''re looking for, and nothing more.

note: this may or may not compile.

#include <stdio.h>
char *filename = "file.txt"
char buffer[100];
FILE *file = fopen(filename, "r");
fgets(file, buffer, 100); // read line until ''\n''
// put this in a loop to read until end-of-file.
// which it returns NULL i believe.
// then simply close the file.
fclose(file);

hope that helps.



This is C right?

.lick

Share this post


Link to post
Share on other sites
"This looks like the kind of homework assignment people get in their first programming class. We won''t do your homework for you.
Any decent book on C++ programming will give you the information you need to read and write files.
Look at the classes ifstream and ofstream.
Yes, you can encrypt files. However, that doesn''t necessarily mean only your program can modify it, as encryptions can be broken and keys can be stolen..."


NO, IT was not my homework!I was just trying to find some help.
I have books, i see...but books dont often describe what i need exactly(i read those chapters-at least this i have dont!).things like making file invisible for instance.Also if i am missing something, i can modify cursor position when i write in a file-but i want it to go to next line, not some random position!anyway, thanks GekkoCube!











No, it was not homework!I was just searching for some help,

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
People here will happily explain an algorithm or technique and in a lot of case they may post code. But asking explicitly for code makes you appear as though you couldn''t be bothered working out how to do it yourself.

Cuting and pasting of code is not the way to go. You won''t learn anything that way.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Look into std::getline, declared in the string header.

int main () {
ifstream fin ("file";
string sin;

while (getline (fin, sin))
{
cout << sin << endl;
}
return 0;
}

Share this post


Link to post
Share on other sites