Thread: structure problems

  1. #1
    Unregistered
    Guest

    structure problems

    Ok here is my structure...

    typedef struct stock_details
    {
    char stckno[3];
    char name[15];
    char description[30];
    char price[6];
    }stock_data;

    that i get...
    then i use fgets and, fseek to get the input and get rid of any null values... like so:

    fgets(stck1.stckno, 2, stdin); //get input
    fseek(stdin, 0, SEEK_END); //dicard xtra
    fgets(stck1.name,14, stdin);
    fseek(stdin, 0, SEEK_END);
    fgets(stck1.description, 29, stdin);
    fseek(stdin, 0, SEEK_END);
    fgets(stck1.price, 5, stdin);
    fseek(stdin, 0, SEEK_END);

    problem is when i input this into a text file the text is all over the place because it is reading enter keys... any way to get rid of them? oh and i already blanked the variables too... eg stockno has " " entered into it futher up my code...

    any help appreciated

  2. #2
    Sy
    Guest
    ok so i tried this way in the hope of getting rid of any \n's (entrptr and c are poiinters... with c = '\n'

    fgets(cus1.name,14, stdin);
    entrptr = strchr(cus1.name, c);

    if (entrptr)
    {
    fseek(stdin, 0, SEEK_END);
    }

    but that doesnt seem to work... am i doing something wrong?

  3. #3
    Sy
    Guest
    the examples are from similar code... two separate modules, imagine cus1.name is stck1.stckno or something

  4. #4
    Sy
    Guest
    Well you helpful bunch... i solved it :P

  5. #5
    Sayeh
    Guest
    Why would you go through all this pain to read/write a structure to disk? Instead of all the fgets, just read and/or write the entire structure _once_ in binary mode?

  6. #6
    Unregistered
    Guest
    would greatly appreciate ur thoughts on directly reading/writing entire structures directly in binary mode....cheers!

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > would greatly appreciate ur thoughts on directly
    > reading/writing entire structures directly in binary
    > mode....cheers!

    Simple stuff:

    fread( &toThisStruct, sizeof( struct ThisStruct ), 1, filePointer );

    If you're reading into an array, replace '&toThisStruct' with the name of the array, and replace the '1' with the number of structures to read.

    fwrite works the same way.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Sy
    Guest
    Originally posted by Sayeh
    Why would you go through all this pain to read/write a structure to disk? Instead of all the fgets, just read and/or write the entire structure _once_ in binary mode?
    because in my project i am creating a customer file of a fixed length... i would use the binary method for something less permanenant like placing an order say... but customers i would rather store on disk

  9. #9
    Sy
    Guest
    for anyone interested here is the final code... the get_string function is particularly useful because it has teh advanteages og fgets yet it nulls any entere characters... veru useful for output to a text file...

    //Add a customer (1.1.1)
    //Will be passed a filename (in this case g_filename)
    //Returns customer number
    //By Simon Taylor

    #include <stdio.h>
    #include <string.h>

    void get_string(char*buf, int length);

    char cus_file[15]="C:\\test.txt";

    typedef struct cus_details
    {
    int account[6];
    char name[16];
    char address[61];
    char postcode[9];
    char tel_no[13];
    }cus_data; //can be treaten as a type of variable (cus_data variable_name)

    int main(void)
    {
    //printf("%s\n\n",g_filename); check filename being passed in

    char yesno;
    FILE*fp;
    fp=fopen(cus_file, "a+");

    if(fp==NULL)
    {
    fprintf(stderr, "Could not open or create file");
    return 1;
    }

    do
    {
    printf("To add a Customer...\n");
    printf("\tType Cus_name\n\tAddress\n\tPostcode\n\tTel_No\n\n");

    cus_data cus1; //cus1 is my new cusdata variable which has lil ones inside
    get_string(cus1.name,15);
    get_string(cus1.address,60);
    get_string(cus1.postcode,8);
    get_string(cus1.tel_no,12);

    fwrite(&cus1.name,15,sizeof(char),fp); //the clever bit, write something size of cus1.name to fp
    fwrite(&cus1.address, 60,sizeof(char),fp);
    fwrite(&cus1.postcode, 8, sizeof(char),fp);
    fwrite(&cus1.tel_no, 12, sizeof(char),fp);

    printf("Add another customer? Y/N");
    scanf("%c%*c", &yesno);

    }while (yesno!='n');

    fclose(fp);

    return 0;
    }


    void get_string(char*buf, int length) //funtion getin cus name and the lenth
    {

    char*entrptr; //pointer for the enter character
    memset(buf,' ',length); //make the (eg, cus1.name) = as many blanks as in length
    fgets(buf,length, stdin); //fill the buffer (eg, cus1.name) up to as many ASCII as in length
    entrptr = strchr(buf, '\n'); //if an enter is found point to it

    if (entrptr!=NULL) //if the pointer is not null
    *entrptr='\0'; //set the char at that location as null
    fseek(stdin, 0, SEEK_END); //discard any input past length
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  2. structure ...solution plz????
    By hegdeshashi in forum C Programming
    Replies: 4
    Last Post: 07-24-2006, 09:57 AM
  3. structure problems in windows program.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 04-19-2004, 06:18 PM
  4. Realloc problems with sturcture array inside structure
    By daveyand in forum C Programming
    Replies: 2
    Last Post: 03-29-2004, 06:48 AM
  5. Structure problems...
    By MillaTime in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 09:27 PM