file I/O questions
i need to know how to access each line of a file once by one and then load each line into a int. the only problem is that each line will be a diff. length..and the lengths can not be hard coded, because they will always be changed prior to each time the file is accessed.
i also need to know how to save a file, like save the values of a couple of diff. ints into one file, each int on a new line.
==============================
whats a signature?
htm[s]l[/s]
whats a signature?
htm[s]l[/s]
A line of what? an array of intergers? If that''s the case you could use the new command ...
int *myints = new [HOW_MANY];
then when the program ends you delete them ...
delete[] myints;
If I had to do this I would probably store the data in binary format, you could start the file with an int indicating how many ints to read forward, then another int, etc. like this:
3 012 4 0123 2 01
So the first line length is 3 ints
Rick
int *myints = new [HOW_MANY];
then when the program ends you delete them ...
delete[] myints;
If I had to do this I would probably store the data in binary format, you could start the file with an int indicating how many ints to read forward, then another int, etc. like this:
3 012 4 0123 2 01
So the first line length is 3 ints
Rick
thats sort of what i want to do...but heres exactly what i want to do:
int myint1;
int myint2;
int myint3;
void main()
{
//read a given filename line by line
line_one_of_file = myint1;
line_two_of_file = myint2;
line_three_of_file = myint3;
}
note: i am working on a Win32 program, not a DOS program, so the file loading function has to be compatable with win32.
int myint1;
int myint2;
int myint3;
void main()
{
//read a given filename line by line
line_one_of_file = myint1;
line_two_of_file = myint2;
line_three_of_file = myint3;
}
note: i am working on a Win32 program, not a DOS program, so the file loading function has to be compatable with win32.
==============================
whats a signature?
htm[s]l[/s]
whats a signature?
htm[s]l[/s]
If it''s an ASCII file, try this:
FILE *fs = fopen("file.txt","r");
int n=0;
while(!eof(fs))
fscanf(" %d ", &array[n++]);
lntakitopi@aol.com | http://geocities.com/guanajam/
FILE *fs = fopen("file.txt","r");
int n=0;
while(!eof(fs))
fscanf(" %d ", &array[n++]);
lntakitopi@aol.com | http://geocities.com/guanajam/
Don''t you mean ...
int myint1 = line_1; // line_1 stored as int in the file
..etc...
myint1 is the variable, line_1 is the one with the value, assigning line_1 to an empty variable is rather pointless =) ... also I don''t see what the question is if each line is sizeof(int) (ie you dont have variable length lines) why dont you save all your ints 1 by 1 then read them back?
Do you need to know how to write an int to a file and then read it back? is that the question? This is the simplest thing, read your help files for fread and fwrite functions.
Rick
int myint1 = line_1; // line_1 stored as int in the file
..etc...
myint1 is the variable, line_1 is the one with the value, assigning line_1 to an empty variable is rather pointless =) ... also I don''t see what the question is if each line is sizeof(int) (ie you dont have variable length lines) why dont you save all your ints 1 by 1 then read them back?
Do you need to know how to write an int to a file and then read it back? is that the question? This is the simplest thing, read your help files for fread and fwrite functions.
Rick
heh...help files: they never *actually* helped anybody.
the help files show you how to init a file to be read/written to. but they dont actually show you how to read/write to them.
the help files show you how to init a file to be read/written to. but they dont actually show you how to read/write to them.
==============================
whats a signature?
htm[s]l[/s]
whats a signature?
htm[s]l[/s]
May 27, 2000 10:11 PM
An simple example of how to save and load an array of integers with a binary file. I mainly did this to brush up on my file I/O skills. It is ANSI C++ so it will run in DOS, WIN32 (maybe not the printf()and getch()), or UNIX. Well enough, here it is:
// Integer saving test
#include
#include
int *array = NULL;
int numOfItems;
void main()
{
// Allocate the array
numOfItems = 10;
array = new int [numOfItems];
// Fill with some values (in this case, squares);
for (int i = 0; i < numOfItems; i++)
{
array = i*i;
}
// Open binary file for writing
FILE *output;
if ((output = fopen("testfile.wvr", "wb")) == NULL)
{
perror("Opening file for writing.\n");
return;
}
// Write number of items
fwrite(&numOfItems, sizeof(int), 1, output);
// Write the array of items
fwrite(array, sizeof(int), numOfItems, output);
// Close the file
fclose(output);
// Deallocate data
delete array;
array = NULL;
numOfItems = 0;
// *** Now reload info from file ***
// Open binary file for reading
FILE *input;
if ((input = fopen("testfile.wvr", "rb")) == NULL)
{
perror("Opening file for reading.\n");
return;
}
// Read number of items
fread(&numOfItems, sizeof(int), 1, input);
// Allocate space in array
array = new int [numOfItems];
// Read the array of items
fread(array, sizeof(int), numOfItems, input);
// Close the file
fclose(input);
// Display the data
printf("File contained %d items.\n", numOfItems);
for (int i = 0; i < numOfItems; i++)
{
printf("Item %d: %d.\n", i, array);<br> }<br><br> // Wait before closing<br> getch();<br><br> // Deallocate data again<br> delete array;<br> array = NULL;<br> numOfItems = 0;<br>}<br> </i>
// Integer saving test
#include
#include
int *array = NULL;
int numOfItems;
void main()
{
// Allocate the array
numOfItems = 10;
array = new int [numOfItems];
// Fill with some values (in this case, squares);
for (int i = 0; i < numOfItems; i++)
{
array = i*i;
}
// Open binary file for writing
FILE *output;
if ((output = fopen("testfile.wvr", "wb")) == NULL)
{
perror("Opening file for writing.\n");
return;
}
// Write number of items
fwrite(&numOfItems, sizeof(int), 1, output);
// Write the array of items
fwrite(array, sizeof(int), numOfItems, output);
// Close the file
fclose(output);
// Deallocate data
delete array;
array = NULL;
numOfItems = 0;
// *** Now reload info from file ***
// Open binary file for reading
FILE *input;
if ((input = fopen("testfile.wvr", "rb")) == NULL)
{
perror("Opening file for reading.\n");
return;
}
// Read number of items
fread(&numOfItems, sizeof(int), 1, input);
// Allocate space in array
array = new int [numOfItems];
// Read the array of items
fread(array, sizeof(int), numOfItems, input);
// Close the file
fclose(input);
// Display the data
printf("File contained %d items.\n", numOfItems);
for (int i = 0; i < numOfItems; i++)
{
printf("Item %d: %d.\n", i, array);<br> }<br><br> // Wait before closing<br> getch();<br><br> // Deallocate data again<br> delete array;<br> array = NULL;<br> numOfItems = 0;<br>}<br> </i>
I think I know exactly what you''re talking about =]
try this:
#include
#include
#define MAX_VALUES 200
int Values[MAX_VALUES];
int numValues;
bool LoadValues(char *FileName)
{
FILE *File;
char Buffer[256];
int Count;
File = fopen(FileName, "r");
if (!File)
return false;
Count = 0;
while (fgets(Buffer, 256, File) != NULL)
{
Values[Count] = atoi(Buffer); // convert char * to int
Count++;
}
numValues = Count;
return true;
}
hope that helped =]
try this:
#include
#include
#define MAX_VALUES 200
int Values[MAX_VALUES];
int numValues;
bool LoadValues(char *FileName)
{
FILE *File;
char Buffer[256];
int Count;
File = fopen(FileName, "r");
if (!File)
return false;
Count = 0;
while (fgets(Buffer, 256, File) != NULL)
{
Values[Count] = atoi(Buffer); // convert char * to int
Count++;
}
numValues = Count;
return true;
}
hope that helped =]
- Ian Perez (iperez.fett@verizon.net) - "It is by will alone I set my mind in motion"
hmm.. on that last post i guess the html didn''t like the less than/greater than symbols
the #includes should have been stdio.h and stdlib.h
oh and at the end (right before ''return true''), you should call fclose(File);
the #includes should have been stdio.h and stdlib.h
oh and at the end (right before ''return true''), you should call fclose(File);
- Ian Perez (iperez.fett@verizon.net) - "It is by will alone I set my mind in motion"
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement