Advertisement

file i/o help

Started by May 29, 2000 07:30 PM
1 comment, last by OoMMMoO 24 years, 7 months ago
I am working on making a password and a saving system for my game. I know how to read from the first line of the file or read the entire file till I get to the end but how do i (for example) read the first line then read the line after that, then maybe read line 50 of the file can someone help?
You can do one of two things:
1. Use random access files or
2. Use an array.

I haven''t the foggiest how to do the first but the second is easy as pie.

Just make a really big array(or a little one if its a little file and start reading in lines of the array.
for example: (I''m a little rusty w/getline but you''ll get the idea)
ifstream fin;
fin.open(filename, ios::in);

int a = 0;
char array[50][80];

while(fin.getline(line1))
{
strcpy(array[a++], line1);
}

now to access an individual line you just go:

strcpy(lineIwant, array[linenumberIwant]);

Hope this helps
Advertisement
FILE *stream = fopen("myfile", "r");
char line[100];
fgets( line, 100, stream );


fgets will read characters from the file and deposit them into line, stopping when it finds a newline or reads 100-1 characters.
Search for "file I/O operations" in the help if you''re using VC++ for a list of more functions available with the "stdio.lib"

This topic is closed to new replies.

Advertisement