Save to file after sorting

  • Thread starter ^cypis^ vel. SQ9JTI
  • Start date
C

^cypis^ vel. SQ9JTI

Hi there,
i have a problem. I prepared bucket sorting with reading values from
file (data.txt) and saving to other file (aus.txt).
Everything is good if we talk about reading and sorting, but saving
doesn't work.
data.txt looks:
4
3
67
15
1
4
13
87

After compiling and running file aus txt lokks like this:
4011840

Where is the failure? Could anyone give me corrected source?
This is my code:

#include <iostream>
#include <conio.h>
#include <string.h>
#include <fstream.h>
//#include <stdio.h>
//using namespace std;

int readData(int numbers[10])
{
FILE *pl;
int i = 0;
pl = fopen("data.txt", "r");
if (pl == NULL)
printf("file not found");
while (1==fscanf(pl, "%d", &numbers)) {
i++;
}

return i;
}

void saveToFile(int table[10],char *name)
{
ofstream pl2;
pl2.open(name);
pl2 << table[10];
pl2.close();
}

int MAX=100;
int bucket[10];

int bucketsort(int numbers[10], int n)
{
int i, m;

i=0;
while(i<MAX) {
bucket = 0;
i=i+1;
}

i=0;
while(i<n) {
bucket[numbers] = bucket[numbers] + 1;
i=i+1;
}

m=0;
i=0;
while(i<MAX) {
while(bucket>0) {
numbers[m] = i;
m=m+1;
bucket = bucket-1;
}
i=i+1;
}

return 0;}

int main(int argc, int* argv[])
{
char file_name;
char *file="aus.txt";
int i,il;
int table[10];
ile = readData(table);
for (i = 0; i < il; i++)
printf("%d ", table);
printf("\n");

bucketsort(table, il);

for (i = 0; i < il; i++)
printf("%d ", table);
printf("\n");
saveToFile(table,file);

return 0;

}
 
M

Marcus Kwok

^cypis^ vel. SQ9JTI said:
Hi there,
i have a problem. I prepared bucket sorting with reading values from
file (data.txt) and saving to other file (aus.txt).
Everything is good if we talk about reading and sorting, but saving
doesn't work.
data.txt looks:
4
3
67
15
1
4
13
87

After compiling and running file aus txt lokks like this:
4011840

Where is the failure? Could anyone give me corrected source?
This is my code:

#include <iostream>
#include <conio.h>

Non-standard header
#include <string.h>
#include <fstream.h>

//#include <stdio.h>
//using namespace std;

Also, I find it a little non-idiomatic to mix C-style FILE*'s with
C++-style fstreams.
int readData(int numbers[10])
{
FILE *pl;
int i = 0;
pl = fopen("data.txt", "r");
if (pl == NULL)
printf("file not found");
while (1==fscanf(pl, "%d", &numbers)) {
i++;
}

return i;
}

void saveToFile(int table[10],char *name)
{
ofstream pl2;
pl2.open(name);
pl2 << table[10];
pl2.close();
}


There is not a suitable default operator<< for arrays that does what you
want. You need to loop through the values in the array and output them
individually (similarly to how you print them to the screen in main()).

Also, you have Undefined Behavior because table is an array with 10
elements (indexed from 0 to 9), so table[10] does not exist. Even if
you corrected it to be table[9], it wouldn't output the entire array to
the file, but only the last element.
int MAX=100;
int bucket[10];

int bucketsort(int numbers[10], int n)
{
int i, m;

i=0;
while(i<MAX) {
bucket = 0;
i=i+1;
}


bucket only has 10 elements as declared, but in this loop you loop 100
times. This is Not Good.
i=0;
while(i<n) {
bucket[numbers] = bucket[numbers] + 1;
i=i+1;
}

m=0;
i=0;
while(i<MAX) {
while(bucket>0) {
numbers[m] = i;
m=m+1;
bucket = bucket-1;
}
i=i+1;
}

return 0;}

int main(int argc, int* argv[])


Should be:

int main(int argc, char* argv[]) // argv is array of char*, not int*
{
char file_name;

Doesn't appear to be used, and is also misleading since a file name
should be longer than 1 character.
char *file="aus.txt";

file should be const char* instead of just char*.
int i,il;
int table[10];
ile = readData(table);
for (i = 0; i < il; i++)
printf("%d ", table);
printf("\n");

bucketsort(table, il);

for (i = 0; i < il; i++)
printf("%d ", table);
printf("\n");
saveToFile(table,file);

return 0;

}
 
J

John Harrison

^cypis^ vel. SQ9JTI said:
Hi there,
i have a problem. I prepared bucket sorting with reading values from
file (data.txt) and saving to other file (aus.txt).
Everything is good if we talk about reading and sorting, but saving
doesn't work.
data.txt looks:
4
3
67
15
1
4
13
87

After compiling and running file aus txt lokks like this:
4011840

Where is the failure? Could anyone give me corrected source?
This is my code:

Your program contains lots of loops. This is good. Now add one more loop
and save the elements of your array ONE AT A TIME. You cannot save a
array all at once, you have to write a loop and save each element
seperately.

john
 
C

^cypis^ vel. SQ9JTI

Hi,
it's workung, this is the code:
------------------------------------------

#include <iostream>
#include <conio.h>
#include <string.h>
#include <fstream.h>
//#include <stdio.h>
//using namespace std;

int readData(int numbers[9])
{
FILE *pl;
int i = 0;
pl = fopen("data.txt", "r");
if (pl == NULL)
printf("file not found");
while (1==fscanf(pl, "%d", &numbers)) {
i++;
}

return i;

}
void saveToFile(int table[9],char *name)
{
ofstream pl2;
pl2.open(name);
for (int i=0;i<9;i++)
{
pl2 << table;
pl2 << "/n ";
}
pl2.close();

}

int MAX=100;
int bucket[9];

int bucketsort(int numbers[9], int n)
{
int i, m;

i=0;
while(i<MAX) {
bucket = 0;
i=i+1;

}

i=0;
while(i<n) {
bucket[numbers] = bucket[numbers] + 1;
i=i+1;

}

m=0;
i=0;
while(i<MAX) {
while(bucket>0) {
numbers[m] = i;
m=m+1;
bucket = bucket-1;

}
i=i+1;
}

return 0;}

int main(int argc, int* argv[])
{
char file_name;
char *file="aus.txt";
int i,il;
int table[9];
ile = readData(table);
for (i = 0; i < il; i++)
printf("%d ", table);
printf("\n");

bucketsort(table, il);

for (i = 0; i < il; i++)
printf("%d ", table);
printf("\n");
saveToFile(table,file);

return 0;

}
-----
It is the newest version. but now i receive srted file with bonus
value: aus.txt. looks like this:

1 3 4 4 13 15 67 87 2009249812

What for is this 2009249812 ?
Thanks for your answers!
Regards,
Luke
 
J

John Harrison

^cypis^ vel. SQ9JTI said:
Hi,
it's workung, this is the code:
------------------------------------------

#include <iostream>
#include <conio.h>
#include <string.h>
#include <fstream.h>
//#include <stdio.h>
//using namespace std;

int readData(int numbers[9])
{
FILE *pl;
int i = 0;
pl = fopen("data.txt", "r");
if (pl == NULL)
printf("file not found");
while (1==fscanf(pl, "%d", &numbers)) {
i++;
}

return i;

}
void saveToFile(int table[9],char *name)
{
ofstream pl2;
pl2.open(name);
for (int i=0;i<9;i++)
{
pl2 << table;
pl2 << "/n ";
}
pl2.close();

}

int MAX=100;
int bucket[9];

int bucketsort(int numbers[9], int n)
{
int i, m;

i=0;
while(i<MAX) {
bucket = 0;
i=i+1;

}

i=0;
while(i<n) {
bucket[numbers] = bucket[numbers] + 1;
i=i+1;

}

m=0;
i=0;
while(i<MAX) {
while(bucket>0) {
numbers[m] = i;
m=m+1;
bucket = bucket-1;

}
i=i+1;
}

return 0;}

int main(int argc, int* argv[])
{
char file_name;
char *file="aus.txt";
int i,il;
int table[9];
ile = readData(table);
for (i = 0; i < il; i++)
printf("%d ", table);
printf("\n");

bucketsort(table, il);

for (i = 0; i < il; i++)
printf("%d ", table);
printf("\n");
saveToFile(table,file);

return 0;

}
-----
It is the newest version. but now i receive srted file with bonus
value: aus.txt. looks like this:

1 3 4 4 13 15 67 87 2009249812

What for is this 2009249812 ?
Thanks for your answers!
Regards,
Luke


If you look at the original file there are EIGHT numbers. If you look at
your code you read NINE numbers. That is where the extra number comes from.

Another issue with you code.
> int MAX=100;
> int bucket[9];
>
> int bucketsort(int numbers[9], int n)
> {
> int i, m;
>
> i=0;
> while(i<MAX) {
> bucket = 0;
> i=i+1;
>
> }


bucket has a size of 9, MAX is 100, therefore this loop is writing to
elements of the array that don't exist. You are lucky that your program
doesn't crash. Perhaps you meant this

int MAX=100;
int bucket[MAX];

John
 
R

red floyd

^cypis^ vel. SQ9JTI said:
Hi,
it's workung, this is the code:
This is a non-standard header and it's contents are unused.
#include <string.h>
This include brings in the strXXX functions. You do not use them.
In addition, you should use <cstring> instead, if you wish to use strXXX.
If you are attempting to use a std::string, then you should
#include said:
#include <fstream.h>
This is a non-standard header and should not be included.
Use #include said:
//#include <stdio.h>
This is needed for fopen, fscanf.
//using namespace std;
You *need* either this, or you need to qualify ofstream with std::.
int readData(int numbers[9])
{
FILE *pl;
int i = 0;
pl = fopen("data.txt", "r");
if (pl == NULL)
printf("file not found");
while (1==fscanf(pl, "%d", &numbers)) {
i++;
}

return i;

Resource leak here, you never fclose p1.
}
void saveToFile(int table[9],char *name)
{
ofstream pl2;
pl2.open(name);
for (int i=0;i<9;i++)
{
pl2 << table;
pl2 << "/n ";
}
pl2.close();

}

int MAX=100;
int bucket[9];

int bucketsort(int numbers[9], int n)
{
int i, m;

i=0;
while(i<MAX) {
bucket = 0;
i=i+1;

}

i=0;
while(i<n) {
bucket[numbers] = bucket[numbers] + 1;
i=i+1;

}

m=0;
i=0;
while(i<MAX) {
while(bucket>0) {
numbers[m] = i;
m=m+1;
bucket = bucket-1;

}
i=i+1;
}

return 0;}

int main(int argc, int* argv[])
{
char file_name;
char *file="aus.txt";
int i,il;
int table[9];
ile = readData(table);
for (i = 0; i < il; i++)
printf("%d ", table);
printf("\n");

bucketsort(table, il);

for (i = 0; i < il; i++)
printf("%d ", table);
printf("\n");
saveToFile(table,file);

return 0;

}
-----
It is the newest version. but now i receive srted file with bonus
value: aus.txt. looks like this:

1 3 4 4 13 15 67 87 2009249812

What for is this 2009249812 ?
Thanks for your answers!
Regards,
Luke
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top