12 Replies - 108163 Views - Last Post: 12 October 2010 - 07:50 AM Rate Topic: ***-- 2 Votes

#1 daffi_gusti   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 10
  • Joined: 08-June 09

read string from text file and storing to array

Posted 10 October 2010 - 01:18 AM

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

Each character must store in an array.

a > to array[0]
b > to array [1]
.
.
.
4 > to array [14]

and the arrays will be showed 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.

Is This A Good Question/Topic? 0
  • +

Replies To: read string from text file and storing to array

#2 bedmond   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 86
  • Joined: 09-October 10

Re: read string from text file and storing to array

Posted 10 October 2010 - 01:41 AM

http://www.cplusplus...g/string/c_str/
Was This Post Helpful? 0
  • +
  • -

#3 daffi_gusti   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 10
  • Joined: 08-June 09

Re: read string from text file and storing to array

Posted 10 October 2010 - 02:04 AM

View Postbedmond, on 10 October 2010 - 12:41 AM, said:



Thanks...
But this do not have the code for splitting the string and save it into array.
Was This Post Helpful? 0
  • +
  • -

#4 bedmond   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 86
  • Joined: 09-October 10

Re: read string from text file and storing to array

Posted 10 October 2010 - 02:23 AM

Is each letter/number in the contents of the file a string or is it just 1 string?
Was This Post Helpful? 0
  • +
  • -

#5 daffi_gusti   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 10
  • Joined: 08-June 09

Re: read string from text file and storing to array

Posted 10 October 2010 - 03:52 AM

View Postbedmond, on 10 October 2010 - 01:23 AM, said:

Is each letter/number in the contents of the file a string or is it just 1 string?


it is a string letter.
Was This Post Helpful? 0
  • +
  • -

#6 Munawwar   User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: read string from text file and storing to array

Posted 10 October 2010 - 04:37 AM

The trick is to use fscanf to get your data.

Say, fscanf(file,"%[^ \n\t\r]s",line); will read all characters that are NOT 'white' space characters (space,newline,tab and line feed characters respectively).

Once you get a text, remove any 'white space' characters that comes after it.
Keep iterating and you get your code.
char line[100];
char junk[100];

FILE *file = fopen("test.txt", "r");
if(!file) {
    printf("Could not open file. Exiting application. Bye");
    return 1;
}
while(!feof(file)) {
    fscanf(file,"%[^ \n\t\r]s",line); //Get text
    printf("%s\n", line);

    fscanf(file,"%[ \n\t\r]s",junk); //Remove any 'white space' characters
}
fclose(file);



EDIT: By the way...anything stopping you from using C++? Cause this can be done easily using C++ streams.

This post has been edited by Munawwar: 10 October 2010 - 04:38 AM

Was This Post Helpful? 1
  • +
  • -

#7 daffi_gusti   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 10
  • Joined: 08-June 09

Re: read string from text file and storing to array

Posted 11 October 2010 - 08:50 AM

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 array
        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.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,666
  • Joined: 25-December 09

Re: read string from text file and storing to array

Posted 11 October 2010 - 09:29 AM

What do you think this line of code is doing?
char* output1[100];



Jim
Was This Post Helpful? 0
  • +
  • -

#9 daffi_gusti   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 10
  • Joined: 08-June 09

Re: read string from text file and storing to array

Posted 11 October 2010 - 09:37 AM

View Postjimblumberg, on 11 October 2010 - 08:29 AM, said:

What do you think this line of code is doing?
char* output1[100];



Jim


I use it to store the result of delimiter string...
output1[i]=result;



this line is worked.

But I cannot use this as array.
I cannot call that array variable.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,666
  • Joined: 25-December 09

Re: read string from text file and storing to array

Posted 11 October 2010 - 09:44 AM

What it is doing is declaring an pointer to an char array[100] not a char *.

What you need is char *output1; See strtok()
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,666
  • Joined: 25-December 09

Re: read string from text file and storing to array

Posted 11 October 2010 - 04:02 PM

Try:

        result = strtok( string, delims1 );
        while( result != NULL )
        {
           printf("%s\n",result);
           result = strtok( NULL, delims1 );
        }



Jim
Was This Post Helpful? 0
  • +
  • -

#12 daffi_gusti   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 10
  • Joined: 08-June 09

Re: read string from text file and storing to array

Posted 11 October 2010 - 05:55 PM

Thanks for your reply.

I think this is not problem, your code will be same result with my code:

         result = strtok( string, delims1 );
     
            while( result != NULL )
            {
                output1[i]=result;
              printf("%s\n",output1[i]);
              result = strtok( NULL, delims1 );
                i++;
     
            }


This code also print out the string correctly.
Print out of my code:
a
b

1
2
4

2
3
1

*3
1
3

*4
4
4

The problem is I want print out that string with an array not by pointer, the code that i've written is I print out that string from pointer. The point is how to change a pointer become an array.

I write this.
 output1[i]
to implement array, but this not affect to the code above at all.

without this,
for (num = 0; num < 100; i++ )      //
       {                                   // Error On this 
          printf("%s\n", output1[i]);     //
       }   

My code run correctly.
But I wanna print out the string with this looping array.
I read from others to implement array from pointer, we have use something code like malloc or calloc.
But I don't know how to implement that function. :helpsmilie: :withstupid:
Was This Post Helpful? 0
  • +
  • -

#13 daffi_gusti   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 10
  • Joined: 08-June 09

Re: read string from text file and storing to array

Posted 12 October 2010 - 07:50 AM

Thank you all, after long time I am try looking to fulfilled that code in C,
I decide to use C++ to implement this...huft.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main()
{
	string str[30][3];
	ifstream myfile("table.txt");
	int a = 0;
	int b = 0;
	if(!myfile) //Always test the file open.
	{
		cout<<"Error opening output file"<<endl;
	    system("pause");
		return -1;
	}
	char nextToken;
	char  array[100];
	while (myfile >> nextToken) {

		array[a] = nextToken;
		//cout << nextToken << endl;
		a++;
	}


  for (int i =0; i < a; i++)
  {
  cout << array[i] << endl;	

}
	 system("pause");
	 return 0;
}





The code will be printed same with above, the string can save in array and print it out one by one.
Thanks all.

This post has been edited by daffi_gusti: 12 October 2010 - 07:54 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1