Dear all,

I am trying to read a list of string from text file and put it into an array.
Could anyone tell me, how to split that string to array.

Here's my text file....
text.txt

a b
1 2 4
2 3 1
*3 1 3
*4 4 4


I want to show that looks like this,

a
b
1
2
4
2
3
1
*3
1
3
*4
4
4

My code:

#include <stdio.h>
#include <stdlib.h>



int main(void) {
int i=0;
char* string[100];
char line[100];

FILE *file; 
file = fopen("text.txt", "r"); 

while(fgets(line, sizeof line, file)!=NULL) 
printf("%s", line);
string[i]=line;
i++;
}

for (i=0 ; i<4; i++) {
printf("\n%s", string[i]);

}
fclose(file);
return 0;
}

my code only show like this.

a b
1 2 4
2 3 1
*3 1 3
*4 4 4

Can anyone tell me, where i have to fix my code. Plz help me.

thanks in advance.

Edited 6 Years Ago by daffi_gusti: n/a

I'm not sure what your trying to accomplish here...Do you want to read each character and display it on its own line?

I quickly wrote this. It will output the characters like you posted

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char**argv)
{
	char ca;
	FILE *fd;

	if (!(fd = fopen(argv[1], "r")))
	{
		fprintf(stderr, "could not open %s\n", argv[1]);
		exit(EXIT_FAILURE);
	}
	
	while (fread(&ca, sizeof(char), 1, fd))
	{
		if (ca != '\n' && ca != ' ')
		{
			if (ca == '*')
			{
				fprintf(stdout, "%c", ca);
			}
			else
			{
				fprintf(stdout, "%c\n", ca);
			}
		}
	}

	fclose(fd);
	return 0;
}

Edited 6 Years Ago by gerard4143: n/a

Thanks for your replied...your code is usefull for me.
But I want the output store in the array, and the array have a certain access whenever we want...

The output will be store in array[];

a array[0]
b array[1]
1 array[2]
2
4
2
3
1
*3
1
3
*4
4
4 array[14]

We can have a certain access to the array, like we want to access array[14] the output will be 4, access array[1] the output will be b, and so on.

Any suggestion how the code looks like:

Thanks.

I have made the code like this

#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("text.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
        printf("%s\n", output1[i]);     //
    }                                   //


    }
printf("\n%d",i/3+1);



    return 0 ;
}

The code contains any error in array declaring.
I do know why the error. Any suggestion? I want to print the string in the array in C code.

line 13 char* output1[100];

this would be pointer to pointer to string i suppose maybe you better make a copy of this string before saving pointer to it ?

works like that bot it still have mistakes(

    #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("text.txt","r");
    //
    while( fgets(string,sizeof(string),f) )
    {
    i=0;
    memset(output1,0,sizeof(output1));
    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; num++ ) // correct would be num < i
    { // Error On thi
    if(output1 != NULL && output1[num] != NULL)
    printf("%s\n", output1[num]); //
    } //
    }
    printf("\n%d",i/3+1);
    return 0 ;
    }

Edited 4 Years Ago by Sokurenko: fix

This article has been dead for over six months. Start a new discussion instead.