473,804 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Automate reading multiple txt files and store it into array

4 New Member
Hi, a C newbie here.

I have been reading threads here about reading txt files into array. I simply copied a couple of useful codes from here and modified to fit my situations.

Then, I have two questions. I have a data set whose data sizes vary. let's say:

data A: 100*20 array;
data B: 180*20 array;
data C: 250*20 array;

Then, I should use fprintf and fscanf in a while loop to read from the original data and write it into a multidimensiona l array, if I understand C correctly...... (should be wrong)

Instead of defining the array size at the beginning in main(), how can I just take the array size from the data itself? I have been looking into a thread posted here a while ago, but I cannot find it. As I recall, it says:

int MAX_SIZE;
int data[MAX_SIZE][MAX_SIZE];

or something to that effect (sorry, not so specific.....)

Second question is "is there any good math/statistical library for C?"
Apr 20 '07 #1
4 2772
Ganon11
3,652 Recognized Expert Specialist
To do this, you'd have to use pointers. You can read the X size as the first number, the Y size as the second number, then make a 2D array from a double-pointer using X and Y. Finally, you can read into the array using fscanf.
Apr 20 '07 #2
taquito
4 New Member
Please forgive me again for this simple question. I have C Primer on my desk, but it only talked about how to read a file as character.

So when I read a txt file like

FILE *fp;
double array[i][j];
int i=0,j=0;
...
...

if((fp=fopen("* **.txt","r")) == NULL)
{
printf("cannot open\n);
exit(1);
}

while
{
for (i=0;i<SIZEX; i++)
for (j=0;j<SIZEY;i+ +)

fscanf(fp,"f",& (array[i][j]);
}

Read the SIZEX as the first number (pointer) means

*SIZEX=sizeof(f p);
?

Thanks.

To do this, you'd have to use pointers. You can read the X size as the first number, the Y size as the second number, then make a 2D array from a double-pointer using X and Y. Finally, you can read into the array using fscanf.
Apr 21 '07 #3
Ganon11
3,652 Recognized Expert Specialist
Reading SIZEX as the first number means that, in your file, you'd have:

Expand|Select|Wrap|Line Numbers
  1. 4
  2. 4
  3. 1 2 3 4
  4. 5 6 7 8
  5. 9 10 11 12
  6. 13 14 15 16
with the first 4 being SIZEX, the next 4 being SIZEY (giving you 4 * 4 = 16 values), and the rest of the numbers being your values.

You'll have to read in SIZEX and SIZEY first, before you get the values with your loops.
Apr 21 '07 #4
taquito
4 New Member
Thank you very much for your help again. I will study a bit more about malloc() and sizeof() in terms of what they do. C program is growing on me very much, and I just want to know more.... What book(s) do you reocommend to beginners and intermediate C programmers. I only have C Primer and Practical C programming at hand. Some of my friends at school recommend Numerical Recipes C. I think I need to take a look at more example codes and learn C on rtial and error. Eventually, I will move on to C++/C#. Although other friends of mine recommend learning Java, Java seems to be a bit more verbal than C/C++.

Again, thanks for your help!!!

Reading SIZEX as the first number means that, in your file, you'd have:

Expand|Select|Wrap|Line Numbers
  1. 4
  2. 4
  3. 1 2 3 4
  4. 5 6 7 8
  5. 9 10 11 12
  6. 13 14 15 16
with the first 4 being SIZEX, the next 4 being SIZEY (giving you 4 * 4 = 16 values), and the rest of the numbers being your values.

You'll have to read in SIZEX and SIZEY first, before you get the values with your loops.
Apr 22 '07 #5

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

Similar topics

16
48891
by: cyranoVR | last post by:
This is the approach I used to automate printing of Microsoft Access reports to PDF format i.e. unattended and without annoying "Save As..." dialogs, and - more importantly - without having to use a commercial program such as Adobe Acrobat and its associated API. The technique uses Ghostscript and Redirection Port Monitor - two free programs for creating PDF documents provided free by Russell Lang. The actual automation requires VBA...
1
1575
by: Mr. B | last post by:
VB.net 2003 c/w Framework 1.1 and MS Access db We have a commercial program that does our Acounting and Time Sheets (Timberline). At least once a day our Accounting department runs a Script file that Exports info into an MS Access db file and which has 7 Tables (stuff like Project info, Project numbers, User names, etc). I wrote a Time Sheet entry application to read this info and to allow Employees to write their Time Sheet data into...
5
2237
by: vj | last post by:
Hi all, I am using C++Builder-5. I want to run multiple cpp files at the same time. If I use the C++Builder for running a cpp file (i.e., I just double click the cpp file, it then opens in the C++Builder, where I have the option to 'Run' it), I can't run multiple files, since the Run option is grayed out if one cpp file is already running. I have another option of running the multiple files: from the command prompt (i.e. opening a...
7
5563
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files, with different file names each time. So i'm writing into the code to ask the user how many files, and what their names are. From each we'll read in 2 lines, then do some math using all of those lines. Then do it again on another set of lines. ...
18
9087
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a database. The files are written by a late 1980's PC Pascal programme, for which I don't have the source code. I've managed to reverse engineer the file format. The strings are stored as Ascii in the file, with the first byte indicating the string...
5
3978
by: cybersangeeth | last post by:
Hi, I need to read 1KB each time from multiple files in a folder and pass it to a byte array in a struct to be sent through a socket. I'm a C++ newbie. I managed to read 1KB each time from one file and store it on to another file using the following code. Could anyone please let me know how i could go about reading from multiple files. Thanks in advance. typedef struct {
6
5278
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
2101
by: pob | last post by:
I currently have a procedure that loops thru a recordset to determine what files need to be loaded to my database. The naming convention of the files has always been accounts.txt, namelist.txt, etc. Within the procedure I also validate that the file loaded to the server was loaded within the current day. Everything has run smooth for quite some time using this method. Now the vendor has told me he has choice, but to the send the file...
4
4899
by: MoroccoIT | last post by:
Greetings - I saw somewhat similar code (pls see link below) that does mupltiple files upload. It works fine, but I wanted to populate the database with the same files that are uploaded to mydirectory, but for some reason, I am getting different file names on the database. Here is the full code: please do serach on kewword "database" to see where I added my database code - that where I need help with. And here the link where I got it...
1
10347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10090
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...
0
9173
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7635
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
6863
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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.