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.

Recommended Answers

All 6 Replies

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

fstream is not C, it's C++

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

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

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 years old 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]

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 == each string line in file

but i get this error in this line :


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

Cannot convert char* to char

but i get this error in this line :

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

Cannot convert char* to char

Hmn! Kind like...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.