473,466 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Read text file into array

40 New Member
Hi ! I know it's possible to use a nested for loop or something to read an array and print it into a text file. Is it also possible to read in a text file and then put the values in an array ? Thanks !
Feb 7 '07 #1
10 31028
Ganon11
3,652 Recognized Expert Specialist
Definitely. What kind of text file will you be reading? What kind of array to you want to generate?
Feb 7 '07 #2
dschulenburg
40 New Member
Definitely. What kind of text file will you be reading? What kind of array to you want to generate?
Cool ! I want to read in a file called file.txt which consists of N rows and N columns filled with values. I will need an array[N][N] that I can then use in code.
Feb 7 '07 #3
Ganon11
3,652 Recognized Expert Specialist
Do you know how many values there will be ahead of time? Is the number of rows different from the number of columns?
Feb 7 '07 #4
dschulenburg
40 New Member
Do you know how many values there will be ahead of time? Is the number of rows different from the number of columns?
Yes, I know the number of rows and columns and they will be equal, so I know that the text file will have e.g. 20*20 values.
Feb 8 '07 #5
Ganon11
3,652 Recognized Expert Specialist
OK, then here's what you want to do:

1) Define a constant MAX_SIZE to the number of rows/columns (in your example, 20)
2) Create a(n) (insert type here) array of size N x N with the statement (int used for example):
Expand|Select|Wrap|Line Numbers
  1. int arr[MAX_SIZE][MAX_SIZE];
3) Create and open your ifstream variable to the input file
4)a. FOR LOOP from 0 to MAX_SIZE - 1 (index r)
Feb 8 '07 #6
Ganon11
3,652 Recognized Expert Specialist
4)b. FOR LOOP from 0 to MAX_SIZE - 1 (index c)
4)c. Input into arr[r][c];

Once the FOR loops finish, your array will be filled with the values from your input file.

Sorry for the 2 posts...the forum error has struck again. It wouldn't let me tack this little bit onto the end of the previous post.
Feb 8 '07 #7
dschulenburg
40 New Member
Thanks, will point 3 and 4 be something like...

while(((c=fgetc(fp) !== EOF)
For (i=0; i <maxsize; i++){
For (j=0; j < maxsize; j++){
c=array[i][j];
}}

??
Feb 8 '07 #8
nmadct
83 Recognized Expert New Member
Thanks, will point 3 and 4 be something like...

while(((c=fgetc(fp) !== EOF)
For (i=0; i <maxsize; i++){
For (j=0; j < maxsize; j++){
c=array[i][j];
}}

??
It should be

Expand|Select|Wrap|Line Numbers
  1. array[i][j] = c;
Also, you aren't accounting for the newline character at the end of each row.

You are using maxsize as the column/row count. This will only work if you're guaranteed to always have the same number of rows/columns. If that count can change, you'll need to count the number of columns in the first row before storing anything. This would be easier if you read the text line-by-line (using fgets) and then iterated over the characters in the line, rather than reading one character at a time.

If you do use fgets, keep in mind that it reads an entire line and leaves the newline character as the last character of the line. BUT, your last line might not have a newline, so don't blindly expect to have a newline unless your file is guaranteed to end with one.
Feb 8 '07 #9
dschulenburg
40 New Member
Thnaks ! So can you alos write an entire line into an array ?

It should be

Expand|Select|Wrap|Line Numbers
  1. array[i][j] = c;
Also, you aren't accounting for the newline character at the end of each row.

You are using maxsize as the column/row count. This will only work if you're guaranteed to always have the same number of rows/columns. If that count can change, you'll need to count the number of columns in the first row before storing anything. This would be easier if you read the text line-by-line (using fgets) and then iterated over the characters in the line, rather than reading one character at a time.

If you do use fgets, keep in mind that it reads an entire line and leaves the newline character as the last character of the line. BUT, your last line might not have a newline, so don't blindly expect to have a newline unless your file is guaranteed to end with one.
Feb 8 '07 #10
nmadct
83 Recognized Expert New Member
Thnaks ! So can you alos write an entire line into an array ?
You can make a loop to write a whole line to the array one character at a time, or you can use a function like strncpy or memcpy to copy the characters in bulk. No matter what you do, just be sure you don't go off the end of the array you're writing to.
Feb 14 '07 #11

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

Similar topics

19
by: ranjeet | last post by:
Hay Guys can you all suggest me the points on the below issue Problem : The thing is that I have the data some thing like this. 1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190,...
7
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
9
by: srikanth | last post by:
i have a text file like below, test.txt file (actually my test file file is with 10000 lines but here i tested with 3 lines) 3 06.09.2006 16:37:25 3 06.09.2006 16:40:02 3 06.09.2006 16:42:31...
6
by: xdeath | last post by:
Hi guys, i've currently got an assignment, whereby, im supposed to create 2 classes, a Vehicle superclass, and a Taxi subclass. Vehicle class needs to have Reg Number, model, price, and Taxi is...
7
by: bowlderster | last post by:
Hello, all. This is the text file named test.txt. 1041 1467 7334 9500 2169 7724 3478 3358 9962 7464 6705 2145 6281 8827 1961 1491 3995 3942 5827 6436 6391 6604 4902 1153 1292 4382 9421 1716...
2
by: RyanS09 | last post by:
Hi- I have read many posts with specific applications of reading in text files into arrays, however I have not been able to successfully modify any for my application. I want to take a text file...
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.