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?"
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.
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.
Ganon11 3,652
Recognized Expert Specialist
Reading SIZEX as the first number means that, in your file, you'd have: - 4
-
4
-
1 2 3 4
-
5 6 7 8
-
9 10 11 12
-
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.
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: - 4
-
4
-
1 2 3 4
-
5 6 7 8
-
9 10 11 12
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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. ...
|
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...
| |
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
{
|
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;
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |