473,395 Members | 1,554 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,395 software developers and data experts.

fread nightmare!

2
Hello everybody! I would be most grateful if anybody could help me out with this one! My code is:
Expand|Select|Wrap|Line Numbers
  1.         int SampleSize=100;
  2.     const char* sample_in = "C:\\test.y";
  3.  
  4.     std::vector<double> Sample;
  5.     Sample.resize(SampleSize);
  6.  
  7.     FILE* stream1;
  8.     size_t numread;
  9.     if ((stream1 = fopen(sample_in,"r")) != NULL)
  10.     {
  11.         numread = fread(&Sample[0],sizeof(double),SampleSize,stream1);
  12.         fclose(stream1);
  13.     }
  14.     else
  15.     { 
  16.         printf("Sample could not be read in");
  17.     }
  18.  
  19.     for (int i=0; i<(int)Sample.size(); i++)
  20.     {
  21.         printf("%1.3f \n",Sample[i]);
  22.     }
  23.  
where test.y contatins a 100-long list of non-zero doubles, making up a sine wave. I have checked that this fil actually contains what i think it does via Matlab and work collegues checking it out for me!

The problem is that what gets printed to the screen is 100 doubles. But only the first 12 are from test.y, and the rest are all 0.00

Does anybody see a mistake in my code? It must be something so stupid that I'm blind to it!!

Thanks in advance,

Deebee
Jan 10 '07 #1
2 1494
deebee
2
It's okay everybody! Crisis averted..

the second fopen param should have been "rb" the b being for binary!!!
stoopid mistake!
Jan 10 '07 #2
RedSon
5,000 Expert 4TB
In the future please remember to use the CODE tags around your blocks of code.

Thanks
Jan 10 '07 #3

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

Similar topics

2
by: Luc Holland | last post by:
Hey, I'm working on a program that reads a binary file. It's opened with ==== if ((f1=fopen(argv,"rb"))==NULL) { fprintf(stderr,"Error opening %s for reading . . .\n",argv); exit(2); } ====...
10
by: Alain Lafon | last post by:
Helas, I got something that should be a minor problem, but anyhow it isn't to me right now. A little code fragment: fread(&file_qn, x, 1, fp_q); The corresponding text file looks like...
6
by: Patrice Kadionik | last post by:
Hi all, I want to make a brief comparison between read() and fread() (under a Linux OS). 1. Family read and Co: open, close, read, write, ioctl... 2. Family fread and Co: fopen, fclose,...
2
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
8
by: M. Åhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)?...
13
by: 010 010 | last post by:
I found this very odd and maybe someone can explain it to me. I was using fread to scan through a binary file and pull bytes out. In the middle of a while loop, for no reason that i could...
5
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a...
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
1
by: Sheth Raxit | last post by:
even thred is not part of C-std, i feel its most relavant to comp.lang.c hope it will not off-topic ------- I am having data/or Text file, which is only used for reading by multiplethreads in...
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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
marktang
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,...
0
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...
0
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,...
0
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...

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.