473,382 Members | 1,204 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

read 1k * 1k data

a
To solve the 100k * 100k data, I finally adopt the file read method and use
malloc. The system somehow knows to use virtual memory.

Now I first test 1k * 1k data but something uninterpretable happens again.

The data file starts with:

42.106983132 85.931514337 98.155213938 23.685776453 76.827067592
....

when I print out the array storing the file content:

0.000000 k: 1020 r: 0.000000 k: 1021 r: 0.000000 k: 1022 r: 0.000000 k: 1023
r: 0.000000 k: 1024 r: 42.106983 k: 1025 r: 85.931514 k:

one can see the first line reading nothing (k as index and r as array
content)

I use the function fscanf to read the file:

#define SIZE 1024

double *r;
int i = 0;

r = (double *)malloc(SIZE * SIZE * sizeof(double));
while(!feof(file) && i<SIZE * SIZE) {
fscanf(file, "%lf", &r[i]);
i++;

// if (i < 30 ) return;
}
for (k=0; k<SIZE*SIZE; k++) {
// if (kSIZE * SIZE - 10 )
if (k< 2048 )
printf("k: %d r: %lf ",k, r[k]);
}
and indeed if (i < 30) return does not stop the file reading loop and i need
to add i < SIZE * SIZE to stop infinite reading. Anybody knows what's
happening?

Furthermore, when I use scanf, fscanf etc to search in Google, it returns
not so helpful example results. I'd also appreciate your suggestions on
searching help on web. Thanks a lot!!
Nov 25 '07 #1
11 1595
a wrote:
To solve the 100k * 100k data, I finally adopt the file read method and use
malloc. The system somehow knows to use virtual memory.

Now I first test 1k * 1k data but something uninterpretable happens again.

The data file starts with:

42.106983132 85.931514337 98.155213938 23.685776453 76.827067592
...

when I print out the array storing the file content:

0.000000 k: 1020 r: 0.000000 k: 1021 r: 0.000000 k: 1022 r: 0.000000 k: 1023
r: 0.000000 k: 1024 r: 42.106983 k: 1025 r: 85.931514 k:

one can see the first line reading nothing (k as index and r as array
content)

I use the function fscanf to read the file:

#define SIZE 1024

double *r;
int i = 0;

r = (double *)malloc(SIZE * SIZE * sizeof(double));
while(!feof(file) && i<SIZE * SIZE) {
This mistake is the subject of Question 12.2 in the
comp.lang.c Frequently Asked Questions (FAQ) list,
<http://www.c-faq.com/>.
fscanf(file, "%lf", &r[i]);
Do you have any reason to believe that this worked?
Question 12.19 doesn't address this issue directly, but
has a few hints about it.
i++;

// if (i < 30 ) return;
}
for (k=0; k<SIZE*SIZE; k++) {
// if (kSIZE * SIZE - 10 )
if (k< 2048 )
printf("k: %d r: %lf ",k, r[k]);
This mistake is the subject of Question 12.9.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Nov 25 '07 #2

"a" <a@a.comwrote in message
To solve the 100k * 100k data, I finally adopt the file read method and
use malloc. The system somehow knows to use virtual memory.

Now I first test 1k * 1k data but something uninterpretable happens again.

The data file starts with:

42.106983132 85.931514337 98.155213938 23.685776453
76.827067592 ...

when I print out the array storing the file content:

0.000000 k: 1020 r: 0.000000 k: 1021 r: 0.000000 k: 1022 r: 0.000000 k:
1023 r: 0.000000 k: 1024 r: 42.106983 k: 1025 r: 85.931514 k:

one can see the first line reading nothing (k as index and r as array
content)

I use the function fscanf to read the file:

#define SIZE 1024

double *r;
int i = 0;

r = (double *)malloc(SIZE * SIZE * sizeof(double));
while(!feof(file) && i<SIZE * SIZE) {
fscanf(file, "%lf", &r[i]);
i++;

// if (i < 30 ) return;
}
for (k=0; k<SIZE*SIZE; k++) {
// if (kSIZE * SIZE - 10 )
if (k< 2048 )
printf("k: %d r: %lf ",k, r[k]);
}
and indeed if (i < 30) return does not stop the file reading loop and i
need to add i < SIZE * SIZE to stop infinite reading. Anybody knows what's
happening?

Furthermore, when I use scanf, fscanf etc to search in Google, it returns
not so helpful example results. I'd also appreciate your suggestions on
searching help on web. Thanks a lot!!
This looks OK to me.
Try putting in a test after malloc() to make sure you actually have the
memory.
Also put a newline on the end of your diagnostic printf().
Then
1) post the code where you open the file
2) try echoing the file characters back to stdout to make sure the file is
being read correctly. You will need to use fgetc().
3) Try reading to a temporary double and printing back to make sure sfcanf()
is converting the information properly.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Nov 25 '07 #3
On Nov 26, 5:24 am, Ben Pfaff <b...@cs.stanford.eduwrote:
"a" <a...@a.comwrites:
#define SIZE 1024
double *r;
int i = 0;
r = (double *)malloc(SIZE * SIZE * sizeof(double));

The most likely result of this size calculation is 8388608. Are
you on a system where `int' can hold this value? `int' may have
a maximum value as small as 32767.
sizeof(double) gives a size_t, so the int that is SIZE*SIZE
= 1048576 will be promoted to size_t for the multiplication.
Nov 25 '07 #4
Old Wolf wrote:
Ben Pfaff <b...@cs.stanford.eduwrote:
>"a" <a...@a.comwrites:
>>#define SIZE 1024
>>double *r;
int i = 0;
>> r = (double *)malloc(SIZE * SIZE * sizeof(double));

The most likely result of this size calculation is 8388608. Are
you on a system where `int' can hold this value? `int' may have
a maximum value as small as 32767.

sizeof(double) gives a size_t, so the int that is SIZE*SIZE
= 1048576 will be promoted to size_t for the multiplication.
No cast needed. However the SIZE * SIZE calculation may be
performed before multiplication by sizeof(double), and that
preliminary may overflow (ints do not have to extend past 32767)
and cause un (or implementation) defined behavior.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Nov 26 '07 #5
"CBFalconer" <cb********@yahoo.coma écrit dans le message de news:
47***************@yahoo.com...
Old Wolf wrote:
>Ben Pfaff <b...@cs.stanford.eduwrote:
>>"a" <a...@a.comwrites:

#define SIZE 1024

double *r;
int i = 0;

r = (double *)malloc(SIZE * SIZE * sizeof(double));

The most likely result of this size calculation is 8388608. Are
you on a system where `int' can hold this value? `int' may have
a maximum value as small as 32767.

sizeof(double) gives a size_t, so the int that is SIZE*SIZE
= 1048576 will be promoted to size_t for the multiplication.

No cast needed. However the SIZE * SIZE calculation may be
performed before multiplication by sizeof(double), and that
preliminary may overflow (ints do not have to extend past 32767)
and cause un (or implementation) defined behavior.
More precisely: SIZE * SIZE shall be performed before multiplication by
sizeof(double) because multiplication is left to right associative. If
MAX_INT is 32767, SIZE * SIZE will overflow, even if size_t happens to be a
larger type.

--
Chqrlie.
Nov 26 '07 #6
"Old Wolf" <ol*****@inspire.net.nza écrit dans le message de news:
05**********************************...oglegroups.com...
On Nov 26, 5:24 am, Ben Pfaff <b...@cs.stanford.eduwrote:
>"a" <a...@a.comwrites:
#define SIZE 1024
double *r;
int i = 0;
r = (double *)malloc(SIZE * SIZE * sizeof(double));

The most likely result of this size calculation is 8388608. Are
you on a system where `int' can hold this value? `int' may have
a maximum value as small as 32767.

sizeof(double) gives a size_t, so the int that is SIZE*SIZE
= 1048576 will be promoted to size_t for the multiplication.
Almost true: the int that is SIZE * SIZE may have overflowed, then the
result will be promoted to size_t before multiplication by sizeof(double).
Unless size_t is a type smaller than int, in which case the whole expression
is evaluated as int and is signed (!).

--
Chqrlie.
Nov 26 '07 #7
In article <47***********************@news.free.fr>, Charlie Gordon
<ne**@chqrlie.orgwrote on Monday 26 Nov 2007 2:32 pm:
"CBFalconer" <cb********@yahoo.coma écrit dans le message de news:
47***************@yahoo.com...
>Old Wolf wrote:
>>Ben Pfaff <b...@cs.stanford.eduwrote:
"a" <a...@a.comwrites:

#define SIZE 1024

double *r;
int i = 0;

r = (double *)malloc(SIZE * SIZE * sizeof(double));

The most likely result of this size calculation is 8388608. Are
you on a system where `int' can hold this value? `int' may have
a maximum value as small as 32767.

sizeof(double) gives a size_t, so the int that is SIZE*SIZE
= 1048576 will be promoted to size_t for the multiplication.

No cast needed. However the SIZE * SIZE calculation may be
performed before multiplication by sizeof(double), and that
preliminary may overflow (ints do not have to extend past 32767)
and cause un (or implementation) defined behavior.

More precisely: SIZE * SIZE shall be performed before multiplication
by sizeof(double) because multiplication is left to right associative.
If MAX_INT is 32767, SIZE * SIZE will overflow, even if size_t happens
to be a larger type.
Couldn't a smart compiler note that the final value must be of type
size_t and hence perform all operations with that type, thus avoiding
the potential int overflow?

Nov 26 '07 #8
"santosh" <sa*********@gmail.coma écrit dans le message de news:
fi**********@aioe.org...
In article <47***********************@news.free.fr>, Charlie Gordon
<ne**@chqrlie.orgwrote on Monday 26 Nov 2007 2:32 pm:
>"CBFalconer" <cb********@yahoo.coma écrit dans le message de news:
47***************@yahoo.com...
>>Old Wolf wrote:
Ben Pfaff <b...@cs.stanford.eduwrote:
"a" <a...@a.comwrites:
>
>#define SIZE 1024
>
>double *r;
>int i = 0;
>
> r = (double *)malloc(SIZE * SIZE * sizeof(double));
>
The most likely result of this size calculation is 8388608. Are
you on a system where `int' can hold this value? `int' may have
a maximum value as small as 32767.

sizeof(double) gives a size_t, so the int that is SIZE*SIZE
= 1048576 will be promoted to size_t for the multiplication.

No cast needed. However the SIZE * SIZE calculation may be
performed before multiplication by sizeof(double), and that
preliminary may overflow (ints do not have to extend past 32767)
and cause un (or implementation) defined behavior.

More precisely: SIZE * SIZE shall be performed before multiplication
by sizeof(double) because multiplication is left to right associative.
If MAX_INT is 32767, SIZE * SIZE will overflow, even if size_t happens
to be a larger type.

Couldn't a smart compiler note that the final value must be of type
size_t and hence perform all operations with that type, thus avoiding
the potential int overflow?
It would not be smart, it would be non conforming.
A smart compiler will detect the overflow in the constant expression and
report it to the programmer with a warning. That is if the programmer is
smart enough to ask for such reports ;-)

--
Chqrlie.
Nov 26 '07 #9
"Charlie Gordon" <ne**@chqrlie.orgwrites:
>>>>>"a" <a...@a.comwrites:
>>#define SIZE 1024
>>
>>double *r;
>>int i = 0;
>>
>> r = (double *)malloc(SIZE * SIZE * sizeof(double));
"santosh" <sa*********@gmail.coma écrit dans le message de news:
fi**********@aioe.org...
>Couldn't a smart compiler note that the final value must be of type
size_t and hence perform all operations with that type, thus avoiding
the potential int overflow?

It would not be smart, it would be non conforming.
Not true: SIZE * SIZE is the product of two `int's. If it
overflows, then behavior is undefined. Thus, the compiler can do
whatever it likes in that case, including evaluate the expression
as type size_t.
--
"You call this a *C* question? What the hell are you smoking?" --Kaz
Nov 26 '07 #10

"Ben Pfaff" <bl*@cs.stanford.eduwrote in message
Not true: SIZE * SIZE is the product of two `int's. If it
overflows, then behavior is undefined. Thus, the compiler can do
whatever it likes in that case, including evaluate the expression
as type size_t.
Whilst if all operands are size_t the behaviour is defined. Therefore the
program cannot print out an error message. However this is a hollow victory
indeed, because the overflow will not do what you want.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Nov 26 '07 #11
Charlie Gordon wrote:
"Old Wolf" <ol*****@inspire.net.nza écrit:
>Ben Pfaff <b...@cs.stanford.eduwrote:
>>"a" <a...@a.comwrites:

#define SIZE 1024

double *r;
int i = 0;

r = (double *)malloc(SIZE * SIZE * sizeof(double));

The most likely result of this size calculation is 8388608. Are
you on a system where `int' can hold this value? `int' may have
a maximum value as small as 32767.

sizeof(double) gives a size_t, so the int that is SIZE*SIZE
= 1048576 will be promoted to size_t for the multiplication.

Almost true: the int that is SIZE * SIZE may have overflowed, then
the result will be promoted to size_t before multiplication by
sizeof(double). Unless size_t is a type smaller than int, in which
case the whole expression is evaluated as int and is signed (!).
No. Once the int overflow occurs things are undefined.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Nov 26 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Ammon | last post by:
I would like to read from a pipe in which data may arrive slowly. From experimenting, it looks like os.read() will block until it returns the maximum amount of data you asked for, in the second...
2
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My...
4
by: francis70 | last post by:
Hi, I have these 2 problem? Is there a way in Oracle to read UNCOMMITED data. i.e. in Oracle the normal behaviour is that a user's updates to a table are visible to other users ONLY when the...
11
by: Markus Breuer | last post by:
I have a question about oracle commit and transactions. Following scenario: Process A performs a single sql-INSERT into a table and commits the transaction. Then he informs process B (ipc) to...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
0
by: Peter | last post by:
I am having a problem reading an Excel file that is XML based. The directory I am reading contains Excel files that can be of two types. Either generic Microsoft based or XML based. I am reading...
0
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
4
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have...
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.