-
arrays
OK,
Is there ANY way to write to /read from a 2-dimensional Array to a text file??
Here's what i got so far:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
main()
{
FILE*wrt;
int a,b,w,x,y,z;
cin>>w>>x>>y>>z;
int grid[2][2]={{w,x}, {y,z}};
for (int i=0; i<2; i++){
for (int I=0; I<2; I++){
wrt=fopen("d:\\cpp\\q", "w");
a=grid[i][I];
fputw(a, wrt);
fclose(wrt);
wrt=fopen("d:\\cpp\\q", "r+");
cout<<fgetw(wrt);
}
}
}
-
The basic flow of your program should be:
declare variables
input data into variables
declare file pointer
open file for writing
use loop(s) to write data to file
open file for reading
use loop(s) for reading file data into array
close file
if you open the file each time through the loop(s) you will probably overwrite the data you previously entered unless you use a different flag. Just open it once for writing and then once for reading. Then close the file.
you really don't need the variable a at all, just use the array indicies to indicate which specific int you are working with.
fputw() doesn't exist, that I remember. fprintf(), fscanf(), fread(), fwrite(), fputc(), fputs() I remember, but not fputw().
It's been quite a while since I used file pointers, but I pretty sure the general theme of my comments still holds even if the details are off somewhat.