-
Arrays and files
I having trouble with a programme im trying to write
I have it so it asks for a file name, opens that file if it exists or comes up with error message if it doesnt.
But what im stuck on it how would i get the content of that file put in to an array or into variables
for example in the file on the first line it says:
25 15
this is the hight of a grid, now i want these to be put into varabiels that are called Height and width, but i am at the moment finding it hard to do so.
This is the code i have so far
Code:
#include <stdio.h>
int main(void)
{
char filename[25];
char wordsearch[50] [50];
char height[50];
int width[50];
FILE *f;
{ /* Code for opening the file */
printf("Please specify input file\n");
scanf("%s", filename);
f = fopen(filename, "r");
while (f==NULL)
{
printf("Couldn't open input file, please change the file name \n");
scanf("%s", filename);
f = fopen(filename, "r");
}
} /* end of code for opening file */
{
fscanf("%s", height);
printf("The height of the grid is... ", height);
}
}
Any help will be greatly appreciated.
-
Not sure what you are shooting for here but would not something like:
int width, height;
...
file opening code here
...
fscanf(fileHandle, "%d %d", &width, &height);
be more like what you want?
-
yeah thats along the lines of it, will try that now
and the code above it just me testing, ignore the array bits on the height and width etc
that was something i forgot to remove lol
-
you've forgotten to include the name of the file pointer:
Code:
fscanf("%s", height); // << eek! >> no file pointer specified
-
this is what i have now:
Code:
#include <stdio.h>
int main(void)
{
char filename[25];
char wordsearch[50] [50];
int width, height;
FILE *f;
{ /* Code for opening the file */
printf("Please specify input file\n");
scanf("%s", filename);
f = fopen(filename, "r");
while (f==NULL)
{
printf("Couldn't open input file, please change the file name \n");
scanf("%s", filename);
f = fopen(filename, "r");
}
} /* end of code for opening file */
{
fscanf(f,"%d %d",width, height);
printf("The height of the grid is... ", height);
printf("The width of the grid is... ", width);
}
}
however when i run that programme it seems to crash on the scanf part, any reason why?? is there something im missing?
-
Remember that to alter the contents of a variable being passed to a function, so that it keeps those changes, you actually have to pass a pointer to that variable; not the value in it:
Code:
fscanf(f,"%d %d",width, height);
Say that you actually initialized 'width' to 5, and 'height' to 10, earlier. This function call as it stands is really something like:
Code:
fscanf(f,"%d %d", 5, 10);
Because you are passing the value of those variables, not their address:
Code:
fscanf(f,"%d %d", &width, &height);
Here you pass the address, because otherwise you couldn't store anything in that variable through the function call. (The real reason is because the function expects pointer values, not just values. The WHY it expects that is explained above.)
Quzah.
-
Well on the fscanf you will need & (ref operator) on the width and height. Not sure why the scanf() is bombing though unless your filename is > 24 chars or you don't have a terminating null in the string (which would manifest itself later, not on scanf()...
-
-
thanks both of you, solved my problem, cant belive i forgot the &, those buggers will be the death of me lol
-
also i was wondering how do you get it to read just the first line, because it seems to be reading the whole of the text document and not outputting anything.
This is the context of the text document:
25 15
xmfycxvtljlqbbybkoumjqwbt
baubmeknbeydqmcnzyjpvrack
qqactivexnyvwdvcoshoyathg
vaghzkctudptjdphsztprbttl
obsnakjwqbouftmgnjqbylinu
vsewohvobxsduqjiffkoylodo
dukwwefroyamapmlrrjvdolop
iqkfxtlksjufmtrsbycmqrrri
afervlqidqxxaoanfqjlmcpjh
yoyywrbpfcalflcbbcoecspwl
twbxetyuyufvvmcuawjmbwlqh
txokdexmdbtgvhpyvsqtmljdx
dcatenrehteoxqdgnueljtrrn
jarteqvtxejfsqddkbuhcysfq
hpdrowssapxtrxhpdxcdhicon
and all i want is for it to read 25, and 15 and then stop.
I was thinking maybe a while loop would be suitable, but wasnt to sure because of it being in a file etc and would know how to write it reallly. Any guidance will be greatly appreciated.
-
Dunno; worked for me once I fixed the missing & in the fscanf and added %ds to your printf() format specifiers..
Code:
jeff@jeff-gate:~/dev/wordsrch$ ./wordsearch_test
Please specify input file
word.data
The height of the grid is... 15
The width of the grid is... 25
jeff@jeff-gate:~/dev/wordsrch$
Oh yeah I did add some \n to the printf()s to clean it up some...
Peace
-
Since you know exactly how many char's you want to read in from the file, I'd use two for loops, and fgetc()
Say the 25 goes into a variable called row, and 15 went into a variable called column. Then you could say:
Code:
//for a 1D array:
total = (25 * 15);
for(i = 0; i < total; i++) {
ch = fgetc(fileHandle);
if(ch == '\n')
--i;
//assign ch into your array at index i
}
for a 2D array, you already have your row and column variables, so
for(r = 0; r < row; r++) {
for(c = 0; c < column; c++)
//same as above, almost
if you only want 25 char's and then 15 char's, just make your
total = (25 + 15) and use the 1D way, above.
If you need 25 char's from the first row, and 15 char's from the second row,
and your rows aren't 25 char's wide, then use fgetc() to get
the first 25 char's from the first row, and then use it again
on the second line to get the 15 char's.