473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reading a Text File Containing Integers

I have a file input.txt which has data like

0 0 0 0 0 0 1
0 0 0 6 0 9 0
0 0 1 0 1 1 0
1 0 0 0 0 4 0
0 0 3 0 1 5 6

I need to read this file and store it in a 2D array.
Plz tell me .. how to do that..
Jan 10 '07 #1
4 3785
Banfa
9,065 Expert Mod 8TB
Look up functions

fopen
fclose
fgets

These will let you read the file into a text string then

strtoul

will let you ares the text data into integer data and you can store it in an array.
Jan 10 '07 #2
I have a file input.txt which has data like

0 0 0 0 0 0 1
0 0 0 6 0 9 0
0 0 1 0 1 1 0
1 0 0 0 0 4 0
0 0 3 0 1 5 6

I need to read this file and store it in a 2D array.
Plz tell me .. how to do that..

2D means 1 for rows , 1 for columns I guess .
If this is so, try the above code, it works also with lines of different lengths like:

0 0 0 0 0 1 1
0 0 0 6 0 9 0 5 4
0 0 1 0 1 1 0 6 6 6
1 0 0 0 0 4 0
0 0 3 0 1 5 6

//---------------------------------------------- C code:

#include <stdlib.h>
#include <stdio.h>

#define M 1000
#define N 500
main ()
{
FILE * fp;
int my_array[M][N];
int i,k,a,maxc;

i=0;
k=0;
maxc=0;
fp = fopen("/input.txt", "rb");
do{
a = fgetc(fp);
my_array[i][k] = a;
i++;
if(a==13){my_array[i][k]='\0';
if(i>maxc)maxc=i;
i=0;
k++;
}
} while((a!=EOF)& (i<M)&(k<N));
fclose(fp);
system("pause");

/*view stored data*/
int j,m;
for(m=0;m<k;m++){
for(j=0; j<maxc;j++) {if(my_array[j][m]!='\0')printf ("%c",my_array[j][m]);}
printf("\n");
}
system("pause");
}

//-----------------------------------------------------------------

Why a==13? this is the ASCII for newline , u can use it to determine where to start a new array row.
Bye!
Jan 12 '07 #3
macklin01
145 100+
Why a==13? this is the ASCII for newline , u can use it to determine where to start a new array row.
Are you sure that works for both "hard" and "soft" returns? i.e., on linjux, a newline is one character (LF), and on DOS / Windows, it's two (CR LF). -- Paul
Jan 12 '07 #4
These are the ASCII for newline depending on OS :
Operating System: Hex ----- Dec
MS DOS/Windows 0Dh 0Ah 13 10
Mac OS-9 &earlier, 0Dh ----- 13
Unix(Linux),MacOSX 0Ah----- 10
Hence the above code will work with Windows and OS9 , just replace:

a==13

with :

a==10

if you are using Unix (Linux) or OSX .
or use an or condition like: (a==13)||(a==10)

for both Unix and Windows

Bye!

Are you sure that works for both "hard" and "soft" returns? i.e., on linjux, a newline is one character (LF), and on DOS / Windows, it's two (CR LF). -- Paul
Jan 12 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: adpsimpson | last post by:
Hi, I have a file which I wish to read from C++. The file, created by another programme, contains both text and numbers, all as ascii (it's a .txt file). A sample of the file is shown below: <<...
0
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
4
by: Matthew Crema | last post by:
Hello, Say I have 1000 text files and each is a list of 32768 integers. I have written a C program to read this data into a large matrix. I am using fopen in combination with fscanf to read...
7
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that...
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
9
by: Eric Lilja | last post by:
Hi! I have a program with a class that needs to be able to write itself to a file in clear text format. The file has two integers and vector of struct objects. The struct has a string that can...
4
by: C++ Newbie | last post by:
Suppose I have a text file with the input: 1 2 3 4 5 6 7 8 9 10 ! Comment: Integers 1 - 10 How do I write a C++ program that reads in this line into a 10-element vector and ignores the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.