Thread: Arrays and files

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    28

    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.

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    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?
    Last edited by jeffcobb; 12-15-2009 at 02:56 PM. Reason: Fixed booboos made before brain engaged...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    28
    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

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    you've forgotten to include the name of the file pointer:

    Code:
    fscanf("%s", height);   //  << eek! >> no file pointer specified

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    28
    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?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    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()...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  8. #8
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    @Quzah: Jinx! ^__^
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    28
    thanks both of you, solved my problem, cant belive i forgot the &, those buggers will be the death of me lol

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    28
    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.

  11. #11
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    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
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global arrays shared between multiple source files
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 08-14-2008, 06:29 PM
  2. C Programming Help - Using Arrays and Files
    By samdog45 in forum C Programming
    Replies: 1
    Last Post: 03-10-2008, 02:18 AM
  3. reading integers into arrays from text files
    By c_beginner in forum C Programming
    Replies: 6
    Last Post: 08-05-2004, 11:42 AM
  4. reading arrays from files
    By iain in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2001, 01:23 PM
  5. writing arrays to files
    By Zaarin in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:26 PM