I am currently having problems reading a list of numbers in from text.
I am trying to read in 100 6-digit numbers from text using fscanf, but the program only reads in 85 for some reason. Anyone have any ideas?
Printable View
I am currently having problems reading a list of numbers in from text.
I am trying to read in 100 6-digit numbers from text using fscanf, but the program only reads in 85 for some reason. Anyone have any ideas?
Using my vast telepathic powers, I've located the problem. Using yours, discern the answer. If you don't have any, then you'll have to post some code.
Quzah.
Here is some code
I am trying to read in from radix1.txt a list of 100 6-digit numbers. I then am going to try to radix sort the whole thing and place the results in radix2.txt
Here is some code.
The problem, so far is that the program only reads in 84 or 85 numbers or so.Code:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void main()
{
int i, x, y, z;
char string1[100][6], string2[100][6];
FILE *inFilePtr;
FILE *outFilePtr;
inFilePtr = fopen("radix1.txt", "r");
outFilePtr = fopen("radix2.txt","w");
if(inFilePtr == NULL)
{
printf("Error opening file.\n");
}//end if
else
{
printf("File opened successfully.\n");
printf("Reading and sorting...");
for (x = 0; x < 115; x++)
{
for (i = 0; i < 6; i++)
{
fflush(stdin);
fscanf(inFilePtr,"%c",&string1[x][i]);
printf("%c", string1[x][i]);
}//end for
}//end for
printf("\nRead successful!\n");
/* Test print */
for (z = 0;z < 115; z++)
{
for (i = 0; i < 6; i++)
{
fprintf(outFilePtr,"%c",string1[z][i]);
}//end for
}//end for
for (z = 5; z > -1; z--)
{
for (y = 0; y < 100; y++)
{
if (string1[y][z] > string1[y + 1][z])
{
for (x = 0; x < 6; x++)
{
string2[y][x] = string1[y][x];
string1[y][x] = string1[y + 1][x];
string1[y + 1][x] = string2[y][x];
}//end for
}//end if
}//end for
}//end for
printf("Sort successful!");
for (z = 0;z < 100; z++)
{
for (i = 0; i < 6; i++)
{
printf("%c",string1[z][i]);
fprintf(outFilePtr,"%c",string1[z][i]);
}//end for
printf("\n");
}//end for
printf("\nDone, results are in radix2.txt");
}//end else
fclose(inFilePtr);
fclose(outFilePtr);
}
fflush(stdin);
Do not flush input streams.
If you're reading strings, you need to include room for the null terminator. Thus, if you have a 6 digit block, you need to allow for 7 characters.
for (x = 0; x < 115; x++)
Why 115? You only have an array that will hold 100. Bad.
fscanf(inFilePtr,"%c",&string1[x][i]);
Your read also leaves a lot to be desired. Is your file one huge consecutive block of numbers? No? Well then, you're going to be reading in your newline characters into the middle of your array. I doubt very much that this is what you want.
That's enough to get you started.
Quzah.
how would you suggest reading in the numbers?
:o
i hate programming