473,749 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ 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 31055
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

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

Similar topics

19
2518
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, 21820, 1730, 2640, 3450, 4870, 6126, 7876, 15644, 17817, 20294, 21902, 2070, 3025, 4333, 5854, 7805, 9231, 10597, 16047........................... soo onnnnnn
7
2264
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
35
11484
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", "r+"); fseek(file, -2, SEEK_END); fscanf(file, "%d", &c); this works fine if the integer is only a single character. When I get into larger numbers though (e.g. 502) it only reads in the 2. Is there
9
5210
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 facing problems: Assume that FILE* filePointer; unsigned char lineBuffer;
9
2022
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 i want to read this and output as it looks but iam getting abnormal
6
2058
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 supposed to inherit those from it. I have done so, but now, it requires that all the data be stored in an array. The final program, should be able to : 1) Add new taxi's into the program (which retains even after program exit) 2) Search for a...
7
3058
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 2718 2895 I wanna to read the data to an array, as the follows:
2
5519
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 filled with a tab delimited list of 10 columns (floats) and read it into a 2D array. The length of the columns are all the same, however this will be variable from text file to text file. Any help (starter code or where to read) would be much...
13
10445
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 set of integers stored in an array.It would be a great help if you could provide some code for it.I tried the function fscanf but by that I am able to read only the first integer of the text file.Please help me.
5
11278
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 couple areas that have ASCII text that I need to extract. At the end of the 2580 bytes, I can read the report like a standard text file. It should have CR/LF at the end of each line. What is the best way for me to read this report using C#. It is...
0
8833
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9389
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4709
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.