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 !
10 31025
Definitely. What kind of text file will you be reading? What kind of array to you want to generate?
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.
Do you know how many values there will be ahead of time? Is the number of rows different from the number of columns?
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.
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): - 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)
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.
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];
}}
??
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
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.
Thnaks ! So can you alos write an entire line into an array ?
It should be
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
by: Naren |
last post by:
Hello All,
Can any one help me in this file read problem.
#include <stdio.h>
int main()
{
|
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",...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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
|
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...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
| |