Thread: Trouble reading from a file

  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    PA
    Posts
    11

    Trouble reading from a file

    Sorry, I know that there are a lot of similar questions but I can't seem to find what the problem is.

    I am just trying to read in values from a text file. They values are separated by tabs.

    I believe the problem is with fscanf. Printf will print the first string correctly but everything after that is messed up.

    Code:
    #include <stdio.h>
    
    struct process {
    	char name;
    	int arrival;
    	int length;
    	int deadline;
    	char type;
    };
    
    int main()
    {
    	// read in all the processes
    	FILE *fr;	// file pointer
    	fr = fopen("input_2011.txt", "rt");  // open the file for reading
    	if (fr == NULL) perror("Error Opening File.");
    	else {
    		struct process SP1;
    		fscanf_s(fr,"%s,%d,%d,%d,%s", &SP1.name, &SP1.arrival, &SP1.length, &SP1.deadline, &SP1.type);
    		printf("%s,%d,%d,%d,%s\n", &SP1.name, SP1.arrival, SP1.length, SP1.deadline, &SP1.type);
    	}
    	fclose(fr);
    	return 0;
    }
    Thank you for any help you can offer.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by awr7126 View Post
    Sorry, I know that there are a lot of similar questions but I can't seem to find what the problem is.

    I am just trying to read in values from a text file. They values are separated by tabs.

    I believe the problem is with fscanf. Printf will print the first string correctly but everything after that is messed up.
    Try it like this....
    Code:
    fscanf_s(fr,"%s\t%d\t%d\t%d\t%s", SP1.name, &SP1.arrival, &SP1.length, &SP1.deadline, SP1.type);
    printf("%s,%d,%d,%d,%s\n", SP1.name, SP1.arrival, SP1.length, SP1.deadline, SP1.type);

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    The field name is just one char. It's not a string.
    You want
    Code:
    struct process {
      char name[MAX_NAME];
      ...
    };

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    The field name is just one char. It's not a string.
    You want
    Code:
    struct process {
      char name[MAX_NAME];
      ...
    };
    Nice catch... I went right past that...

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Then for SP1.type the corresponding format should be %c.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Then for SP1.type the corresponding format should be %c.
    Well, yes, unless it's supposed to be a string too....

  7. #7
    Registered User
    Join Date
    Dec 2010
    Location
    PA
    Posts
    11
    I did mean both 'name' and 'type' to be strings.

    That worked, Thank you so much!

    However it is still giving me 2 warning:
    scheduling.c:43: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[4]’
    scheduling.c:43: warning: format ‘%s’ expects type ‘char *’, but argument 6 has type ‘char (*)[3]’

    Do you have any suggestion on how to eliminate that?

    Code:
    #include <stdio.h>
    
    struct process {
    	char name[4];
    	int arrival;
    	int length;
    	int deadline;
    	char type[3];
    };
    
    int main()
    {
    	// read in all the processes
    	FILE *fr;	// file pointer
    	fr = fopen ("input_2011.txt", "rt");  // open the file for reading
    	if (fr == NULL) perror("Error Opening File.");
    	else {
    		struct process SP1;
    		//fscanf(fr,"%s,%d,%d,%d,%s", &SP1.name, &SP1.arrival, &SP1.length, &SP1.deadline, &SP1.type);
    		fscanf(fr,"%s\t%d\t%d\t%d\t%s", SP1.name, &SP1.arrival, &SP1.length, &SP1.deadline, SP1.type);
    		printf("%s,%d,%d,%d,%s\n", &SP1.name, SP1.arrival, SP1.length, SP1.deadline, &SP1.type);
    	}
    	fclose(fr);
    
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    		printf("%s,%d,%d,%d,%s\n", &SP1.name, SP1.arrival, SP1.length, SP1.deadline, &SP1.type);
    Drop those two ampersands. The name of an array is a pointer to the first element, which is what printf wants for a %s: just a char pointer.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    The printf and scanf functions are fundamentally different.

  10. #10
    Registered User
    Join Date
    Dec 2010
    Location
    PA
    Posts
    11
    Alright, great! Thanks for all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. having trouble reading into Struct from File
    By bcianfrocca in forum C++ Programming
    Replies: 9
    Last Post: 09-06-2005, 10:54 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM