Thread: what's wrong with this

  1. #1
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70

    Exclamation what's wrong with this

    I wrote this program by the lunch time, compiled at:

    http://www.delorie.com/djgpp/compile/

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    typedef struct{
    char usr[20];
    char pwd[20];
    int num;
    }USR;
    int main(int argc, char **argv){
    USR new;
    FILE * log;
    srand(time(NULL));
    printf("n\n\n\t\tEnter Username: ");
    gets(new.usr);
    system("cls");
    printf("n\n\n\t\tEnter Password: ");
    gets(new.pwd);
    new.num = 1 + rand() % 999;
    if((log = fopen("user.by","w+"))){
    printf("Standard Storage File Failure\a\n");
    exit(EXIT_SUCCESS);
    }else{
    printf("Processing.....");
    system("dir");
    if((fprintf(log,"%d\t%s\t%s\n",new.num,new.usr,new .pwd))){
    printf("new user *%s* created...\n",new.usr);
    }else{
    printf("Unable to create user...\n");
    }
    }
    fclose(log);
    return 0;
    }

    the file created by the program is called "user.by" when opened there is nothing on it. i will try to use fflush and create an app to read this file.
    Ünicode¬>world = 10.0£

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You forgot to check if the log filepointer is NULL.
    Change:

    if((log = fopen("user.by","w+"))){

    To:

    if((log = fopen("user.by","w+")) == NULL){

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM