5 Replies - 563 Views - Last Post: 10 October 2012 - 09:14 PM Rate Topic: -----

#1 ofolaran  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-October 12

reading in data from file input output *NO STRING.H*

Posted 10 October 2012 - 12:28 PM

Hi like I said in the description we are not allowed to use the string.h library. The assignment pretty much is you are given a list of countries and the amount of olympic medals they have won, given in the format:

(#gold) (#silver) (#bronze) (country name)
example
23 21 22 Programming Land
12 4 6 Bosnia

this is my code, I am not sure but I think I have an infinite loop, it compiles but when I run it on Putty it just doesn't do anything, doesn't close either. It creates the output file, and i am relatively certain it is reading the input file, but there is nothing being written in the output file.


// Olajubu Folaranmi Section 0103 Week 7

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    int gold, silver, bronze, i, k, EOF_reached;
    char country[30];
    char c;
    
    //declare files
    FILE *data, *output;
   
    // open files and check for errors
    data = fopen(argv[1], "r");
    if (data == NULL)
    { printf("File %s cannot open!\n", argv[1]);
        exit(0);
    }

    output = fopen(argv[2], "w");
    
        
        
		while (fscanf(data, "%d %d %d", &gold, &silver, &bronze) != EOF){
            if (gold > 0 || silver > 0 || bronze > 0){
        i = 0;
		while(i <= 31){
			EOF_reached = fscanf(data, "%c", &c);
			if(c == '\n' || EOF_reached == EOF){
				country[i] = '\0';
				break;
			}
			else{
				country[i] = c;
			}
			i++;
        }
                k=0;
                while (k<=i){
                    fprintf(output, "%c", country[k]);
                    k++;
                }
                    
		}
		}
        

    
    
    fclose(data);
    fclose(output);
    return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: reading in data from file input output *NO STRING.H*

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: reading in data from file input output *NO STRING.H*

Posted 10 October 2012 - 03:26 PM

You can help yourself by either running your code in debugger and tracing through the code, or by adding printf()'s to report the progress of your program to the console.
Was This Post Helpful? 2
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: reading in data from file input output *NO STRING.H*

Posted 10 October 2012 - 03:38 PM

while(i <= 31){


Consider using sizeof(country) here to avoid a buffer overrun (country's only valid indexes are 0-29. In fact, you should consider sizeof(country) - 1 so there will be room for the required null terminator. Actually, because you're not using the string library, you don't really need to worry about the null terminator.

while (fscanf(data, "%d %d %d", &gold, &silver, &bronze) != EOF)


Consider adding a space at the end of that format specifier to eat the whitespace separating the medal count from the identifier. Also, using EOF is problematic.

while (k<=i)

Another buffer overrun.
Was This Post Helpful? 1
  • +
  • -

#4 ofolaran  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-October 12

Re: reading in data from file input output *NO STRING.H*

Posted 10 October 2012 - 07:35 PM

is there another way to read to the end of a file of unknown length other than EOF?
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: reading in data from file input output *NO STRING.H*

Posted 10 October 2012 - 07:58 PM

Yes, you make it a memory mapped file, but that will probably be too advanced for you.

The next option is to discover the length. You can call fstat(), or seekg() to the end of the file, and use tellg() to figure out the size of the file.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,966
  • Joined: 19-February 09

Re: reading in data from file input output *NO STRING.H*

Posted 10 October 2012 - 09:14 PM

You are writing a null character ('\0') to a text file. You could check for the null character and print a newline.


39      fprintf(output, "%c", country[k]);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1