954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Read a Regular .txt file

Hello to All
i want to read txt file that contant some data's such as :

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


and then write them into a two dimentional Array .

for example : first line of file must be first row of array or another example the first data in first line is data[0][0]

somebody say me , that i can do this with use of fstream function or somebody else says that i must use strtok

now i know ,who can i do this ?
Thanks in advance.

Para2x
Newbie Poster
3 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

fstream is not C, it's C++. What language are you writing this program in?

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
fstream is not C, it's C++


i dont know this . i want to write my program in C .
with Turbo C 3 .

Para2x
Newbie Poster
3 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Be clear about your requirement:
1)How many fileds each line will contain.
2)How many lines the file will contain.
3)What all operations you will do after reading data from file

After getting these things clear u can design your code.
still if u need to read each field den
1)get lines by fgets()
2)after getting the like use strtok(line,",") to get each fields.

Then you know what to do with the fileds.

THanks
DP

Dream2code
Junior Poster
144 posts since Jun 2009
Reputation Points: 22
Solved Threads: 12
 

Welcome Para2x,
In your post #3, you said
i want to write my program in C .
with xxxxx C 3
Your compiler is 13 to 15 yearsold standard compiler. Now, it's time to change your compiler. There are number of free C++ compilers.
Read more about standard C/C++ - http://siddhant3s.googlepages.com/how_to_tell_rusted_cpp.html
http://david.tribble.com/text/cdiffs.htm
https://developer.mozilla.org/en/C___Portability_Guide
Free-compilers : CodeBlocks, Dev-C++, Visual C++ 2008.

And also you should have to read,
1. Homework policy
2. How to post source code?
Show your work first. Do not forget to use code tags. Source code must be surrounded with code tags.
For example,

[CODE=c]
... statements..
[/CODE]

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 
1)How many fileds each line will contain. 2)How many lines the file will contain. 3)What all operations you will do after reading data from file

1-i dont know , just user know , but i know that s it beetwen 0-50
2- i dont know too , 0-50

all of my problem is here , that i can split one line by strtok function ,but when i have many lines i cant do this .
,rest is a very smiple , i konw .

i wrote this code but ...

static const char filename[] = "data.txt";
FILE *file = fopen ( filename, "r" );
int i = 0,j=0,num[50]={0},rownum=0;
char arra[50][50];
char line[50]; /* or other suitable maximum line size */
char seps[] = ",",*cha;
char *token;
char strArr[40][40],data[50][50];

/*
.read datas from file
.
/*


for(i=0;i<rownum;i++){

token = strtok(arra[i], seps);

while (token)
{
    strcpy(strArr[i], token);

    token = strtok(NULL, seps);

    i++;
  }
for(j=0;j<num[i];j++)

data[i][j]=strArr[j];

}


arra[i] == each string line in file

but i get this error in this line :

data[i][j]=strArr[j];


Cannot convert char* to char

Para2x
Newbie Poster
3 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

but i get this error in this line :

data[i][j]=strArr[j];

Cannot convert char* to char


Hmn! Kind like...

Attachments 211872_overflowing_glass_1.jpg 11.57KB
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You