LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-26-2005, 05:35 PM   #1
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Rep: Reputation: 15
Reading from a txt file into a two dimension array in C


I'm trying to read from a txt file into a two dimension array. The text file contains following data.

Code:
0 0 0 1 0 0 1 0 1 0 0 1
1 0 1 0 1 0 1 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0
1 1 0 1 0 1 0 1 1 1 1 1
I want to create a two dimensional array (in this case 4X12 array) by reading the txt file.
Can anyone give me a hint on how to do this?

Thanks
 
Old 11-26-2005, 06:07 PM   #2
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
my code looks something
Code:
if (fp == NULL) exit(EXIT_FAILURE);
     for (i=0; i<r; i++)
         for (j=0; j<c; j++){
             fread(a[i][j], 1, c, fp);
                                 }
                       
   }
of course it doesn't compile.
 
Old 11-26-2005, 06:30 PM   #3
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Rep: Reputation: 31
something like this
Code:
man fopen
man fgets
man fscanf
man fclose
These should help you. It's not al all difficult.
 
Old 11-26-2005, 07:04 PM   #4
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
I used fscanf to solve the problem...
Problem solved!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a txt file in terminal over a network mdlister Programming 5 03-14-2005 07:47 AM
c++ writing to a .txt file xemous Programming 8 09-01-2004 10:57 PM
Perl Script Reading a txt file and generate html to be published! kofi Linux - Software 1 09-22-2003 05:12 PM
How to convert a txt file to be a db file in Redhat linux 9? winnie Linux - Newbie 3 06-27-2003 08:33 AM
perl reading pdf,ps,txt j-ray Programming 1 02-04-2003 10:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration