reading a random grid from file into an array

This is a discussion on reading a random grid from file into an array within the C Programming forums, part of the General Programming Boards category; Hello, basically trying to store characters in a grid from a file in to an array i can compile this ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    9

    reading a random grid from file into an array

    Hello,
    basically trying to store characters in a grid from a file in to an array
    i can compile this without any errors
    but won't run
    maybe a misconception?
    any help would be great. thanks.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
     int i = 0,j=0;
     FILE *f;
    
     f = fopen("grid1.txt","r");
    
     char a[10][10];
    
     for(i=0;i<10;i++)  {
         for(j = 0; j < 10; j++)
         {
             fscanf(f,"%s", a[i][j]);
         }
     }
    
    
    /*test output*/
     printf ("\n%s\n",a[0][0]);
    
    }
    sample input file

    Code:
    asfgasfffa
    asfgasfffa
    asfgasfffa
    asfgasfffa
    asfgasfffa
    asfgasfffa
    asfgasfffa
    asfgasfffa
    asfgasfffa
    asfgasfffa

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by seandil666 View Post
    Hello,
    basically trying to store characters in a grid from a file in to an array
    i can compile this without any errors
    but won't run
    maybe a misconception?
    any help would be great. thanks.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
     int i = 0,j=0;
     FILE *f;
    
     f = fopen("grid1.txt","r");
    
     char a[10][10];
    
     for(i=0;i<10;i++)  {
         for(j = 0; j < 10; j++)
         {
             fscanf(f,"%s", a[i][j]);
         }
     }
    
    
    /*test output*/
     printf ("\n%s\n",a[0][0]);
    
    }
    sample input file

    Code:
    asfgasfffa
    asfgasfffa
    asfgasfffa
    asfgasfffa
    asfgasfffa
    asfgasfffa
    asfgasfffa
    asfgasfffa
    asfgasfffa
    asfgasfffa
    a[i][j] is a char not a string. To read strings replace it with a[i] only.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    Quote Originally Posted by BEN10 View Post
    a[i][j] is a char not a string. To read strings replace it with a[i] only.
    thanks for that...

    now I have just seperated some code(searching) into a function, and doesnt work anymore (can compile, would run until the searching starts)
    im sure the problem is in the function parameters
    not sure why pointers are used, as yet - as a beginner

    I have marked the places which are of pain..

    any help would be great..

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
     	int i=0,j=0;
     	FILE *f;
    
     	f = fopen("grid1.txt","r");
    
     	char a[50][50];
    
    	int width, height;
    
        fscanf(f,"%d %d",&width,&height);
    
    	fclose(f);
    	f = fopen("grid1.txt","r");
    
    	for(i=0;i<height+2;i++)  {
         	for(j = 0; j < width && fscanf(f,"%s",a[i])==0; j++)
        	{
             	fscanf(f,"%s",a[i]);
        	}
     	}
    
    	for(i=2;i<height+2;i++)  {
         	for(j = 0; j < width ; j++)
        	{
             	printf("%c",a[i][j]);
        	}
        	printf("\n");
     	}
     	wordSearch("england", a, width, height);
    }
    
    wordSearch(char *word[], char *a[50][50], int width, int height)
    {
    	int i,j,x,d,l,b,c;
    	x=strlen(*word);
    	for(i=2;i<height+2;i++)  {
         	for(j = 0; j < width ; j++)
        	{
             	if(word[0]==a[i][j])
             	{
    				c=c+1;
    				if(width-j>x-1)
    				{
    					d=j+1;
    
    					for(l=1;l<x;l++)
    					{
    						if(word[l]==a[i][d])
    						{
    								b=b+1;
    								d=d+1;
    								if(b==x-1)
    								{
    								    printf("horizontal match");
    								    b=0;
    								}
    						}
    					}
    				}
    			}
        	}
    
    	}
    	for(j = 0; j < width ; j++)  {
         	for(i=2;i<height+2;i++)
        	{
             	if(word[0]==a[i][j])
             	{
    				c=c+1;
    				if(height-i>x-1)
    				{
    					d=i+1;
    
    					for(l=1;l<x;l++)
    					{
    						if(word[l]==a[d][j])
    						{
    								b=b+1;
    								d=d+1;
    								if(b==x-1)
    								{
    								    printf("match");
    								    b=0;
    								}
    						}
    					}
    				}
    			}
        	}
    	}
    }

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    You're passing "england" which is 1D string literal, so you should have the function as
    Code:
    void wordSearch(char *word, char *a[50][50], int width, int height)
    and then
    strlen(word);
    char *word: word is a pointer pointing to char *.
    char *word[]: word is an array containing char *.

    By the way when you're using char *word[], what do you think word[1] would be?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    Quote Originally Posted by BEN10 View Post
    You're passing "england" which is 1D string literal, so you should have the function as
    Code:
    void wordSearch(char *word, char *a[50][50], int width, int height)
    and then
    strlen(word);
    char *word: word is a pointer pointing to char *.
    char *word[]: word is an array containing char *.

    By the way when you're using char *word[], what do you think word[1] would be?
    i tried it. but I get errors now about 'pointer integer comparisons'?? :/
    any ideas? how to go about this..
    thanks

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
     	int i=0,j=0;
     	FILE *f;
    
     	f = fopen("grid1.txt","r");
    
     	char a[50][50];
    
    	int width, height;
    
        fscanf(f,"%d %d",&width,&height);
    
    	fclose(f);
    	f = fopen("grid1.txt","r");
    
    	for(i=0;i<height+2;i++)  {
         	for(j = 0; j < width && fscanf(f,"%s",a[i])==0; j++)
        	{
             	fscanf(f,"%s",a[i]);
        	}
     	}
    
    	for(i=2;i<height+2;i++)  {
         	for(j = 0; j < width ; j++)
        	{
             	printf("%c",a[i][j]);
        	}
        	printf("\n");
     	}
     	wordSearch("england", a, width, height);
    }
    
    wordSearch(char *word, char *a[50][50], int width, int height)
    {
    	int i,j,x,d,l,b,c;
    	x=strlen(word);
    	for(i=2;i<height+2;i++)  {
         	for(j = 0; j < width ; j++)
        	{
             	if(word[0]==a[i][j]) warning: comparison between pointer and integer
             	{
    				c=c+1;
    				if(width-j>x-1)
    				{
    					d=j+1;
    
    					for(l=1;l<x;l++)
    					{
    						if(word[l]==a[i][d])
    						{
    								b=b+1;
    								d=d+1;
    								if(b==x-1)
    								{
    								    printf("horizontal match");
    								    b=0;
    								}
    						}
    					}
    				}
    			}
        	}
    
    	}
    	for(j = 0; j < width ; j++)  {
         	for(i=2;i<height+2;i++)
        	{
             	if(word[0]==a[i][j])
             	{
    				c=c+1;
    				if(height-i>x-1)
    				{
    					d=i+1;
    
    					for(l=1;l<x;l++)
    					{
    						if(word[l]==a[d][j])
    						{
    								b=b+1;
    								d=d+1;
    								if(b==x-1)
    								{
    								    printf("match");
    								    b=0;
    								}
    						}
    					}
    				}
    			}
        	}
    	}
    }

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    char word is only going to be a char mate, even if you name the variable 'word'

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Again you're doing the same mistake. Here:
    Code:
    void wordSearch(char *word, char *a[50], int width, int height)
    Here is a piece of code which will help you in your case. This passes parameters like in yours:
    Code:
    #include <stdio.h>
    #include<string.h>
    void vish(char *word,char *s[])
    {
    	int i;
    	for(i=0;i<3;i++)
    	if(*word==*s[i]) //compares 'e' with each of the first alphabet of the strings
    		printf("\nTRUE");// print TRUE 2 times
    }
    int main() {
    	char *ch[]={"egg",
    		"what",
    		"empire"
    	};
        vish("england",ch);
    	return 0;
    }
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    Quote Originally Posted by rogster001 View Post
    char word is only going to be a char mate, even if you name the variable 'word'
    having fun?
    why cant you just correct my code!!

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    that was a good hint i thought! i am not knocking you man, its just the other post asked you to check what you thought char would be...

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    Quote Originally Posted by rogster001 View Post
    that was a good hint i thought! i am not knocking you man, its just the other post asked you to check what you thought char would be...
    its a good hint alright- except its alll a big riddle for me atm.
    thx for the help anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Real problems reading from a text file into an array
    By cherryduck in forum C Programming
    Replies: 11
    Last Post: 11-04-2009, 10:51 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21