file i/o dilemma

This is a discussion on file i/o dilemma within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #define FILE_NAME "~/CSC60/lab5/out.txt" #define MAX_SIZE 29 #define SIZE 3 typedef struct { int idNumber; char ...

  1. #16
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define FILE_NAME "~/CSC60/lab5/out.txt"
    #define MAX_SIZE 29
    #define SIZE 3
    
    typedef struct {
            int idNumber;
            char name[MAX_SIZE],testScore[SIZE], programScore[SIZE];
            char cLvl_gender, age;
    }student;
    imho i count 41 bytes based on the struct declaration above so you sure have some bits leftover.

  2. #17
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    okay well i made them all unsigned ints but i am having issues with the fwrite statement:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "student.h"
    
    void addStudent(){
            student tmp;
            char tmper, dummy[80];
            FILE *FILE_PTR;
            FILE_PTR = fopen(FILE_NAME,"ab+");
            printf("Please enter their 9-digit id number: ");
            scanf("%d", &tmp.idNumber);
            printf("\nPlease enter their name: ");
            fgets(dummy, sizeof(dummy), stdin);
            gets(tmp.name);
            printf("\nPlease enter their class level: ");
            fgets(dummy, sizeof(dummy), stdin);
            scanf("%c", &tmper);
            tmp.classLvl = tmper;
            printf("\nPlease enter their gender: ");
            scanf("%c", &tmper);
            tmp.gender = tmper;
            printf("\nPlease enter their age: ");
            scanf("%d", &tmp.age);
            fwrite(&tmp, sizeof(student), 1, FILE_PTR);//error's here
            fclose(FILE_PTR);
    }//end addStudent
    and here is the revised header file:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define FILE_NAME "~/CSC60/lab5/out.txt"
    #define MAX_SIZE 29
    #define SIZE 3
    
    typedef struct {
            unsigned int idNumber;
            unsigned char name[MAX_SIZE],testScore[SIZE], programScore[SIZE];
            unsigned char age;
            unsigned int classLvl : 3, gender : 1;
    }student;
    
    void addStudent();
    
    void deleteStudent(unsigned int);
    
    void addTestScore(unsigned int);
    
    void addProgramScore(unsigned int);
    
    void outputClassReport();
    it seems to not like my fwrite line i tried it both with a pointer and with a regular struc assignment and not a structure pointer and it will all error on this line..

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by adramalech View Post
    okay well i made them all unsigned ints but i am having issues with the fwrite statement:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "student.h"
    
    void addStudent(){
            student tmp;
            char tmper, dummy[80];
            FILE *FILE_PTR;
            FILE_PTR = fopen(FILE_NAME,"ab+");
            printf("Please enter their 9-digit id number: ");
            scanf("%d", &tmp.idNumber);
            printf("\nPlease enter their name: ");
            fgets(dummy, sizeof(dummy), stdin);
            gets(tmp.name);
            printf("\nPlease enter their class level: ");
            fgets(dummy, sizeof(dummy), stdin);
            scanf("%c", &tmper);
            tmp.classLvl = tmper;
            printf("\nPlease enter their gender: ");
            scanf("%c", &tmper);
            tmp.gender = tmper;
            printf("\nPlease enter their age: ");
            scanf("%d", &tmp.age);
            fwrite(&tmp, sizeof(student), 1, FILE_PTR);//error's here
            fclose(FILE_PTR);
    }//end addStudent
    and here is the revised header file:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define FILE_NAME "~/CSC60/lab5/out.txt"
    #define MAX_SIZE 29
    #define SIZE 3
    
    typedef struct {
            unsigned int idNumber;
            unsigned char name[MAX_SIZE],testScore[SIZE], programScore[SIZE];
            unsigned char age;
            unsigned int classLvl : 3, gender : 1;
    }student;
    
    void addStudent();
    
    void deleteStudent(unsigned int);
    
    void addTestScore(unsigned int);
    
    void addProgramScore(unsigned int);
    
    void outputClassReport();
    it seems to not like my fwrite line i tried it both with a pointer and with a regular struc assignment and not a structure pointer and it will all error on this line..
    What's wrong with it -- or at least what does your compiler think is wrong with it, or whatever.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Exactly what error do you get?

    I hacked your code around a bit to remove the // comments, not use bitfields.

    Using this:
    Code:
     
    typedef struct {
            unsigned int idNumber;
            unsigned char age;
            unsigned char classLvl, gender;
            char name[MAX_SIZE],testScore[SIZE], programScore[SIZE];
    }student;
    takes exactly 44 bytes (using gcc-mingw 3.4.x). No need for bitfields.

    However, to store age in a byte, you need a temporary variable for scanf, otherwise the scanf will store the data in the wrong place (because scanf assumes that you give it the right arguemnts - but an unsigned char is only 1 byte, a unsigned long is 4 bytes, so it will overwrite 3 bytes of whatever is after that field.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    thanks for the clarification i was confused that i would need to use a bitfield operations to make sure it has the right size.

    the error i get is this: 0x00c1fc38 in fwrite () from /lib/tls/i686/nosegneg/libc.so.6

    maybe it has to do with the bitfield problem???

  6. #21
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    The bitfields are not part of the struct student but are part of the struct rec.
    Trace the call stack; compile with -g0 and do a bt in gdb and post it here.

  7. #22
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    it has nothing to do with bitfield operations as i have tried both ways and none of them seem to make anything different in the error same thing....

  8. #23
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    What platform are you on? Maybe you have an older or bad version of the libc shared library.

  9. #24
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    okay so i still have the error from the fwrite i found out it wasn't the student structure that i was writing to it but more the file stream wasn't substantiated i even tried to open the binary file in the main method just to substantiated it....using this:

    Code:
    FILE *FILE_PTR;
    FILE_PTR = fopen(FILE_NAME, "wb+");
    if(FILE_PTR == NULL){perror("File stream was not opened!"); return 0;}
    fclose(FILE_PTR);
    those were the four lines before i even started anything...the FILE_NAME variable is substantiated as FILE_NAME = "~CSC60/lab5/out"; in the header file...

    here is the gcc -v output:

    Code:
    > gcc -v
    Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.6/specs
    Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
    Thread model: posix
    gcc version 3.4.6 20060404 (Red Hat 3.4.6-10)
    Code:
    Script started on Mon 10 Nov 2008 06:50:50 PM PST
    > ls
    addP.c  addS.c]  addT.o     deleteS.o  outputC.o  test.c
    addP.o  addS.o   a.out*     Makefile*  script     typescript
    addS.c  addT.c   deleteS.c  outputC.c  student.h
    > gdb a.out
    GNU gdb Red Hat Linux (6.3.0.0-1.159.el4rh)
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/tls/i686/nosegneg/libthread_db.so.1".
    
    (gdb) run a.out
    
    Starting program: /gaia/class/student/thronej/CSC60/lab5/a.out a.out
    Please enter the number of the option you wish to run:
    1. add student
    2. delete student
    3. add test score
    4. add program score
    5. output class status
    6. quit
    1
    Didn't open file!: No such file or directory
    Please enter their 9-digit id number: 1111
    
    Please enter their name: aijowfaiowjf
    
    Please enter their class level: 11
    
    Please enter their gender: m
    
    Please enter their age: 21
    
    Program received signal SIGSEGV, Segmentation fault.
    0x00c1fc38 in fwrite () from /lib/tls/i686/nosegneg/libc.so.6
    (gdb) bt
    #0  0x00c1fc38 in fwrite () from /lib/tls/i686/nosegneg/libc.so.6
    #1  0x080488e2 in addStudent () at addS.c:27
    #2  0x08048d7d in main () at test.c:16
    (gdb) fr 1
    #1  0x080488e2 in addStudent () at addS.c:27
    27              fwrite(&tmp, sizeof(student), 1, FILE_PTR);
    (gdb) quit
    The program is running.  Exit anyway? (y or n) y
    > exit
    exit
    
    Script done on Mon 10 Nov 2008 06:51:31 PM PST
    the only thing i switched in addS.c was putting the fopen at the top and then just adding in perror to catch the non-opening of the stream...

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm not completely convinced that ~ is interpreted in the C library. Leave out the path, since it's the same path you're running in, and see what happens.

  11. #26
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    In the filename component if "CSC60" is not the username the tilde in "~CSC60/lab5/out" will not expand to the full pathname of the file.
    Replace the symbolic constant with the complete pathname to the output file and then run your program as in
    Code:
    #define FILE_NAME "/home/username/lab/CSC60/lab5/out.txt"
    use code tags well and don't post long run-on lines as it's very hard on the eyes.

  12. #27
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    > FILE_PTR = fopen(FILE_NAME,"ab+");
    Always check your errors.

    Had you done a test such as this
    if ( FILE_PTR == NULL )

    you would have seen a specific problem, rather than some random looking crash.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #28
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    okay that seemed to be my issue i just made the file name now just "out" because it ignores the .txt extention and will write to the binary file

    my issue is now making sure that i correctly output the structure and that it can do the other function correctly

    i thank everyone for there help and response ....without finishing this i would never have been able to do anything else....

Page 2 of 2 FirstFirst 12
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, 09:35 AM
  2. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-31-2004, 12:21 AM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM

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