problems reading in from text

This is a discussion on problems reading in from text within the C++ Programming forums, part of the General Programming Boards category; I am currently having problems reading a list of numbers in from text. I am trying to read in 100 ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    10

    problems reading in from text

    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?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    10
    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.
    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);
    }
    The problem, so far is that the program only reads in 84 or 85 numbers or so.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    10
    how would you suggest reading in the numbers?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    10

    i hate programming

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 01:23 AM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Reading text file and structuring it..
    By Killroy in forum C Programming
    Replies: 20
    Last Post: 11-19-2004, 08:36 AM
  4. reading integers into arrays from text files
    By c_beginner in forum C Programming
    Replies: 6
    Last Post: 08-05-2004, 12:42 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21