read each word in file and store into array C

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 13687 Views - Last Post: 06 October 2011 - 05:35 AM Rate Topic: -----

#1 guidely  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 18
  • Joined: 05-October 11

read each word in file and store into array C

Posted 05 October 2011 - 12:30 AM

Hi, I have Implement C to test to read each word in file and store into array C but I only can do read by line, I dont know how to read each word and store to array by using Str

here is my code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>

int main ( void )
{
static const char filename[] = "data";
FILE *file = fopen ( filename, "r" );
int i, j;
char arra[128][128];
char line[128]; /* or other suitable maximum line size */


for(i=0; i<128; i++)
for(j=0; j<128; j++)
arra[i][j] = '\0';

for(i=0; i<128; i++)
line[i] = '\0';

if ( file != NULL )
{

i=0;

while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
{

strcpy(arra[i], line);
printf("array ----> %s ", &arra[i]);
i++;

}
fclose ( file );
}
else
{
perror ( filename ); /* why didn't the file open? */
}


return 0;
} 



my data file
3 Aqaba
49789 10000 5200 25.78
6987 148976 12941 15.78
99885 35262 2501 22.98



Thank

Is This A Good Question/Topic? 0
  • +

Replies To: read each word in file and store into array C

#2 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: read each word in file and store into array C

Posted 05 October 2011 - 01:00 AM

Use an indentation style. This makes it easier for other humans to read your code and provide you with help faster. Your code with an indentation style should look something like this.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>

int main ( void )
{
    static const char filename[] = "data";
    FILE *file = fopen ( filename, "r" );
    int i, j;
    char arra[128][128];
    char line[128]; /* or other suitable maximum line size */


    for(i=0; i<128; i++)
        for(j=0; j<128; j++)
            arra[i][j] = '\0';

    for(i=0; i<128; i++)
        line[i] = '\0';

    if ( file != NULL )
    {

        i=0;

        while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
        {

            strcpy(arra[i], line);
            printf("array ----> %s ", &arra[i]);
            i++;

        }
        fclose ( file );
    }
    else
    {
        perror ( filename ); /* why didn't the file open? */
    }


    return 0;
}




Why do you include ctype.h twice?
Was This Post Helpful? 0
  • +
  • -

#3 guidely  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 18
  • Joined: 05-October 11

Re: read each word in file and store into array C

Posted 05 October 2011 - 05:36 AM

View Posthulla, on 05 October 2011 - 01:00 AM, said:

Use an indentation style. This makes it easier for other humans to read your code and provide you with help faster. Your code with an indentation style should look something like this.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>

int main ( void )
{
    static const char filename[] = "data";
    FILE *file = fopen ( filename, "r" );
    int i, j;
    char arra[128][128];
    char line[128]; /* or other suitable maximum line size */


    for(i=0; i<128; i++)
        for(j=0; j<128; j++)
            arra[i][j] = '\0';

    for(i=0; i<128; i++)
        line[i] = '\0';

    if ( file != NULL )
    {

        i=0;

        while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
        {

            strcpy(arra[i], line);
            printf("array ----> %s ", &arra[i]);
            i++;

        }
        fclose ( file );
    }
    else
    {
        perror ( filename ); /* why didn't the file open? */
    }


    return 0;
}




Why do you include ctype.h twice?


it display error message here
test.c:34: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[128]’
test.c:34: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[128]’



and how to ready word by word in each file and store into array

Thank a lot
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,317
  • Joined: 25-December 09

Re: read each word in file and store into array C

Posted 05 October 2011 - 05:42 AM

In the following line:
printf("array ----> %s ", &arra[i]);
You do not need the ampersand.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 guidely  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 18
  • Joined: 05-October 11

Re: read each word in file and store into array C

Posted 05 October 2011 - 05:48 AM

View Postjimblumberg, on 05 October 2011 - 05:42 AM, said:

In the following line:
oh yeh I didnt see , thank
printf("array ----> %s ", &arra[i]);
You do not need the ampersand.

Jim

And how to split each word on line because now it output like this
array ----> 3 Aqaba
 array ----> 49789 10000 5200 25.78
 array ----> 6987 148976 12941 15.78
 array ----> 99885 35262 2501 22.98



I wan to make it like this
3 Aqaba
49789
10000
5200
25.78
6987
...
...



I know we need to include str but I not understand how to actually write into this code
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,317
  • Joined: 25-December 09

Re: read each word in file and store into array C

Posted 05 October 2011 - 06:12 AM

Is there more lines like the first line? Please show the exact layout of your input file.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 guidely  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 18
  • Joined: 05-October 11

Re: read each word in file and store into array C

Posted 05 October 2011 - 06:12 AM

I have try to do it give error like this

Here is my code


#include <stdio.h>
#include <string.h>

main (int argc, char *argv[])
{
    
    FILE *f;
    
    
    int i=0;
    int j=0;
    char output[100];
    char* output1[100];
    char string[100];
    char delims1[] = " ";
    char delims2[] = "*";
    char* result = NULL;
    char* result3 = NULL;
    int num;
    
    
    
    //for (j=0; j<2; j++)
    //{
    //printf("%s",delims9[6]);
    //}
    
    f = fopen("data.txt","r");
    //
    while( fgets(string,sizeof(string),f) )
    {
        
        result = strtok( string, delims1 );
        
        while( result != NULL )
        {
            output1[i]=result;
            printf("%s\n",output1[i]);
            result = strtok( NULL, delims1 );
            i++;
            
        }
        
        for (num = 0; num < 100; i++ )      //
        {                                   // Error On this array
            printf("%s\n", output1[i]);     //
        }                                   //
        
        
    }
    printf("\n%d",i/3+1);
    
    
    
    return 0 ;
}




output should output like this
3 Aqaba
49789
10000
5200
25.78
6987
...
...




but it give me this
3
Aqaba

(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
????
????
??uwA?G
p?H??H???H?I??
??lf?
H?p
??lf?
??lf?
??lf
??t?A?D$I?A??E9?r?1?H?}??U?H?M?I??M??H??H[A\A]A^A_]??Z
(null)
????


Segmentation fault: 11



This post has been edited by guidely: 05 October 2011 - 06:19 AM

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,317
  • Joined: 25-December 09

Re: read each word in file and store into array C

Posted 05 October 2011 - 06:30 AM

I don't think you will need strtok() to read this file. You will probably want to look up sscanf() to break apart the lines.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 guidely  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 18
  • Joined: 05-October 11

Re: read each word in file and store into array C

Posted 05 October 2011 - 06:41 AM

View Postjimblumberg, on 05 October 2011 - 06:30 AM, said:

I don't think you will need strtok() to read this file. You will probably want to look up sscanf() to break apart the lines.

Jim


But I want to load those data into array as well and and data can be more than the line that data.txt is contain. fo sscanf it just break for each one into variable but if I got like 50 line it going to a lot of variable right?
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,594
  • Joined: 16-October 07

Re: read each word in file and store into array C

Posted 05 October 2011 - 06:53 AM

// you define 100 char *
char* output1[100];

// ac actual buffer to store data
char string[100];

// and one char *
char* result = NULL;

// load data into the buffer
while( fgets(string,sizeof(string),f) ) {
	// you get a pointer to the result
	result = strtok( string, delims1 );
	while( result != NULL ) {
		// you reference a pointer
		output1[i]=result;
		// that will go away soon
		// gone
		result = strtok( NULL, delims1 );
	}

	// num or i, pick one
	for (num = 0; num < 100; i++ ) {
		// print a now invalid pointer
		// is is probably wrong
		printf("%s\n", output1[i]);



You have a file with data. You want to read the file into an array, then print it out again.

What data is in the input file? How do you want to store it? How do you want to print it?

Is that three at the start of your data meaningful? Three records, three columns, what?
Was This Post Helpful? 0
  • +
  • -

#11 guidely  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 18
  • Joined: 05-October 11

Re: read each word in file and store into array C

Posted 05 October 2011 - 07:02 AM

Yeh I wan to do something with data later, but first store each element into array exam like this array[0] = 3Aqaba array[1] = 49789 is like separate each element by space. for ever first line the datatype should be string (3Aqaba) , and other ( 49789 10000 5200 ) is integer and for every last element of each line from second line below is double(25.78), for output I just want to test is it working or not because I wanna do something with that data later the only part I cant do is only separate each word then store to array


3Aqaba

49789 10000 5200 25.78

6987 148976 12941 15.78

99885 35262 2501 22.98

Thank
Was This Post Helpful? 0
  • +
  • -

#12 guidely  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 18
  • Joined: 05-October 11

Re: read each word in file and store into array C

Posted 05 October 2011 - 07:41 AM

Anyone ? give me a start up
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,317
  • Joined: 25-December 09

Re: read each word in file and store into array C

Posted 05 October 2011 - 08:16 AM

Quote

But I want to load those data into array as well and and data can be more than the line that data.txt is contain. fo sscanf it just break for each one into variable but if I got like 50 line it going to a lot of variable right?

Why can't the variable be an element in an array?

Does the file contain more than one line like "3Aqaba"? Please show a larger sample of your input file.

Jim
Was This Post Helpful? 0
  • +
  • -

#14 guidely  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 18
  • Joined: 05-October 11

Re: read each word in file and store into array C

Posted 05 October 2011 - 08:27 AM

View Postjimblumberg, on 05 October 2011 - 08:16 AM, said:

Quote

But I want to load those data into array as well and and data can be more than the line that data.txt is contain. fo sscanf it just break for each one into variable but if I got like 50 line it going to a lot of variable right?

Why can't the variable be an element in an array?

Does the file contain more than one line like "3Aqaba"? Please show a larger sample of your input file.

Jim


here is all my input file
3Aqaba
2234 	10000	20000	12.50
2235 	12400	24800	22.78
2236 	8300	        16600	34.55
2237	        11786	12500	14.34
2238  	16901	25023	9.78
2239	         19076	38678	23.06
2240	        14238	24356	127.08
2241 	511	         1256	13.50
2242  	45403	85000	137.00
2243	        600	          1000	17.86



but it can be more when i past it in here it not lineup properly

This post has been edited by guidely: 05 October 2011 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,594
  • Joined: 16-October 07

Re: read each word in file and store into array C

Posted 05 October 2011 - 08:29 AM

If your code always follows the format of "49789 10000 5200 25.78" then a sscanf as previously suggested is even more appropriate. e.g. "%d %d %d %f".
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2