Skip to main content
gamedev.net
🔒 Locked

c++ write

Started by Filip Sep 4, 2003 at 4:57 AM 7 replies 1.2k views
Original Post
Filip
Filip
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!
fizban75
fizban75
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...
GekkoCube
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.
Pipo DeClown
Pipo DeClown
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
Filip
Filip
"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,
C-Junkie
C-Junkie
you seem to have this idea that file HAVE lines. they don''t. you just interpret a ''\n'' character as an end-of-line marker.
brassfish89
brassfish89
Any decent c++ book should have that information.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.