|
From: Kristian M. <kri...@ya...> - 2007-07-20 16:21:59
|
Yes, the fopen lacked the "b" flag. Now it's working fine, Thanks a lot!
I agree that the code mixes C and C++ I/O, but file I/O is done just with C-like file I/O, isn't it? C++ file I/O would imply in use of stream classes...
Regarding the reading / writing integers, is there a way to specify the byte order, or should my code itself decompose the integers into bytes (using shifting operations and bit masks)?
Thanks again.
----- Original Message ----
From: Per Westermark <pw...@ia...>
To: Kristian Magnani <kri...@ya...>
Cc: dev...@li...
Sent: Thursday, July 19, 2007 7:29:20 PM
Subject: Re: [Dev-C++] fread get stuck
It's a bit hurtfull to my eyes to see source code that mixes C and C++
file I/O. I would recommend that you use one or the other.
In your case, you have forgotten an important difference between Windows
and Unix. In Windows, there is a difference between binary and ASCII
files. When writing to a file opened in ASCII mode, any character "\n"
will be converted to "\r\n" (CR+LF), and when reading to a file opened in
ASCII mode, the caracters "\r\n" will be converted to a "\n".
In your case, you are using fread() and fwrite() to read and write as if
the integers are binary data. That requires you to add a "b" to the mode
string of fopen().
Note that you are using a non-portable way if reading and writing
integers. The data is read and written in the byte order of the machine
you are currently using. A better way to create binary data files is to
beforehand specify a byte order, and then write the integers as separate
bytes.
/pwm
On Thu, 19 Jul 2007, Kristian Magnani wrote:
> Hi, all!
>
> I've been using gcc in linux for a while, but now I'm trying do compile some example codes I've under Dev-C++, and I'm facing some problems.
>
> Below follow two programs: one opens a file where it write 1000 random numbers and the other open a file to read numbers and print them into the screen. Thet work perfectly under gcc/linux, but at Dev-C++/windows the program that reads the file get stuck at a particular number. For the same file, it always stops reading at the same line, although for different files, it stops at different lines.
>
> I suspect it's something defined as open behaviour in C/C++ specification, because the same programs work fine at gcc/linux, but I don't know what. Does anyone have an idea what I'm doing wrong?
>
> Thanks!
>
>
>
> #include <iostream>
> #include <stdlib.h>
>
> using namespace std;
>
> int main (int argc, char *argv[])
> {
> /* inicializa gerador de numeros aleatorios */
> srand(time(NULL));
>
> /* abre arquivo para escrita, criando se nao existir */
> /* cursos posicionado no inicio do arquivo */
> FILE* file = fopen(argv[1], "w+");
> if (file == NULL)
> {
> cout << "Erro ao criar arquivo." << endl;
> }
> else
> {
> for (int i = 0; i < 1000; i++)
> {
>
> /* pausa execucao */
> if (i % 100 == 0 && i > 0) cin.get();
>
> /* escreve um numero aleatorio no arquivo aberto */
> int aleatorio = rand() % 1000;
> cout << i + 1 << "\tEscrevendo '" << aleatorio
> << "' no arquivo '" << argv[1] << "'." << endl;
> int elementosEscritos = fwrite(&aleatorio, 1, sizeof(int), file);
> if (elementosEscritos < 0)
> {
> cout << "Erro ao escrever em arquivo.";
> }
> }
> }
>
> /* fecha arquivo */
> fclose(file);
>
> /* pausa execucao */
> cin.get();
>
> return 0;
> }
>
>
>
>
>
>
> #include <iostream.h>
> #include <stdlib.h>
>
> using namespace std;
>
> int main (int argc, char *argv[])
> {
>
> /* abre arquivo para leitura */
> /* cursos posicionado no inicio do arquivo */
> FILE* file = fopen(argv[1], "r+");
> if (file == NULL)
> {
> cout << "Erro ao abrir arquivo '" << argv[1] << "'." << endl;
> }
> else
> {
> int i = 0;
> int elementosLidos;
> int aleatorio;
>
> /* le um numero do arquivo aberto */
> while (elementosLidos = fread(&aleatorio, sizeof(int), 1, file) > 0)
> {
> i++;
> cout << i << "\tLeu '" << aleatorio
> << "' (" << elementosLidos << " elementos) do arquivo '"
> << argv[1] << "'." << endl;
> }
> }
>
> /* fecha arquivo */
> fclose(file);
>
> /* pausa execucao */
> cin.get();
>
> return 0;
> }
>
>
>
>
>
>
> ____________________________________________________________________________________
> Moody friends. Drama queens. Your life? Nope! - their life, your story. Play Sims Stories at Yahoo! Games.
> http://sims.yahoo.com/
____________________________________________________________________________________Ready for the edge of your seat?
Check out tonight's top picks on Yahoo! TV.
http://tv.yahoo.com/ |