Kristian Magnani
2007-07-19 21:08:55 UTC
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/
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/