You are here:

C++/read data from a file in c++

Advertisement


Question
i want a read word by word from a text file and store it in a array. looks simple, but its a problem for me. please help me .

Answer
Hi Ramachandra

Create a structure
struct word {
char data[20];
};

Open the file using read mode
FILE *fp = fopen(filename,"r+");

Now read word by word and store in structure
char ch[20];
while(fp!=eof)
fscanf(fp,"%s",ch);

Now the words from your file will come one after the other inside the variable ch now you can do whatever you like with them and can simply store them in the above structure.

Do lemme know in case you are stuck with any problem.

regards
Joydeep Bhattacharya
http://www.scodz.com

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joydeep Bhattacharya

Expertise

TSR, Socket and Very Complex Mathematical Problem

Experience

Worked on many OOP projects with design patterns

Organizations
http://www.scodz.com

Publications
http://www.scodz.com

Education/Credentials
Master of Computer Applications

Awards and Honors
Being a proud owner of a website of programming called http://www.scodz.com

Past/Present Clients
http://www.funforu.com http://www.dhoondho.com http://analysingc.50webs.com

©2012 About.com, a part of The New York Times Company. All rights reserved.