File I/O help for new programmer

This is a discussion on File I/O help for new programmer within the C Programming forums, part of the General Programming Boards category; I have a homework program that i need some help with. I am supposed to read information from a file ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    14

    File I/O help for new programmer

    I have a homework program that i need some help with.

    I am supposed to read information from a file and sort it according to the id. the data in the file looks like this
    1001 joe 100.50

    the id is the first field the name is the second and the dollar amount is the 3rd. I have tried using fgets to read the information in. it works so far but i dont know where to go from there. I need to do some error checking on the data that is read in as well.. such as
    id should be between 1000-9999
    name should be 10 or less characters
    id should be unique.

    any help with this is appreciated..

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    create a structure that stores the id, name, and dollar amount.

    Then make an array of those structures.

    After using fgets() use sscanf() to load the data into the structure.

    to verifiy you could create a function kinda like this:
    Code:
    int verified (array_of_the_structure temp[], int index, int size)
    {
      int unique=1;
      int counter;
      for (counter=0; counter < size; counter++)
        if ( temp[index].id == temp[counter].id )
        {
          unique=0;
          break;
        }
      return unique;
    }
    For sorting look up selection sort as it will be the easist for you to understand and implament at this point.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    I forgot to mention that we arent allowed to use structures at this point. thanks for the suggestion though

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    If you can use arrays then you can have one array for id, one for name, and one for dollar amount.

    Same basic principle applies

  5. #5
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Post Using sscanf()

    Hi jayslilhunter,

    Assuming you already have the file read in the buffer, I have some techniques that can do what you are looking for.

    Use sscanf() .

    Code:
    #include <stdlib.h>
    
    void main() {
    	char	name[10], *buffer = "1001 joe 100.50";
    	int	idNum;
    	float	amount;
    
    	// replace "buffer" with your input line
    
    	sscanf(buffer, "%i %s %g", &idNum, name, &amount);
    	name[10] = '\0';	// stop name at 10th byte
    
    	printf("ID: %i\n", idNum);
    	printf("Name: %s\n", name);
    	printf("Amount: $%g", amount);
    }
    Code 1.1: Reading parts of a string

    Now, to answer your question about the name being 10 characters or less, I made sure no matter how long the name is (max of 128) the name stops at the 10th byte setting it to the NULL Terminator.

    Also you can read more information on sscanf() and printf()

    Simply what sscanf() does is it looks for information inside a string, like integers, floats and etc... Also %i converts into an integer, %g into a float, and %s into a string. More format modifiers are explained on the above links.

    Hope this helps,
    - Stack Overflow
    Last edited by Stack Overflow; 04-05-2004 at 01:00 PM. Reason: Forgot to include headers
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >#include <stdlib.h>
    You meant <stdio.h> I think.

    >void main()
    main returns int

    >name[10] = '\0'; // stop name at 10th byte
    You just wrote to memory you don't own. Anything could happen at this point.

    I would go with something a little more robust:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      char *buffer = "1001 joe 100.50";
      int id;
      char name[11];
      double amount;
    
      if ( sscanf ( buffer, "%4d %10[^0123456789] %lf", &id, name, &amount ) == 3 ) {
        if ( id > 999 )
          printf ( "ID: %d\nName: %s\nAmount: %.2f\n", id, name, amount );
      }
    
      return 0;
    }
    >the name stops at the 10th byte
    The 11th character according to your code. Remember that arrays are 0 based, so the last valid element in an array of size 10 is at the 9th index.
    My best code is written with the delete key.

  7. #7
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    Can someone explain this line please:
    Code:
    if ( sscanf ( buffer, "%4d %10[^0123456789] %lf", &id, name, &amount ) == 3 )
    I've seen this somewhere before, but just not sure what it means.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I've seen this somewhere before
    You have my condolences.

    >but just not sure what it means.
    [^...] means to read the longest non-empty string of characters not between the brackets. [...] means to read the longest non-empty string of characters between the brackets. So %10[^0123456789] will read up to 10 characters as long as they are not digits and then terminate the string with '\0'.
    My best code is written with the delete key.

  9. #9
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    Direct replies to duplicate post here.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 03:47 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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