file somethingsomething

This is a discussion on file somethingsomething within the C Programming forums, part of the General Programming Boards category; heres a piece of my code : FILE *f; f=fopen("dump.df","w"); if(!f) { printf("Could not open dump.df\n"); return 1; } i ...

  1. #1
    Unregistered
    Guest

    Unhappy file somethingsomething

    heres a piece of my code :

    FILE *f;
    f=fopen("dump.df","w");
    if(!f)
    {
    printf("Could not open dump.df\n");
    return 1;
    }

    i was wondering if there is a possible way of making the filename
    >> "dump.df" being asked from the user ?

    something like:

    scanf("%s", ??);
    FILE *f;
    f=fopen("%s","w"), ??;
    if(!f)
    {
    printf("Could not open dump.df\n");
    return 1;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      FILE *f;
      char buffer[BUFSIZ];
      printf ( "Enter a filename: " );
      if ( fgets ( buffer, (int)sizeof buffer, stdin ) != NULL ) {
        if ( ( f = fopen ( buffer, "r" ) ) != NULL ) { 
          /* Do your thing */
        }
        else
          perror ( "fopen failure: variable f" );
      }
      else
        perror ( "fgets failure: variable buffer" );
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    hmmmmm.... thats odd. it only gives me errormessages when i run it

  4. #4
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Code:
    FILE *f;
    char filename[255];
    
    puts(" Enter Filename: ");
    gets(filename);
    f=fopen(filename,"w"); 
    if(!f) 
    { 
    printf("Could not open %s\n", filename); 
    return 1; 
    }

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Unregistered
    hmmmmm.... thats odd. it only gives me errormessages when i run it
    That's because the fgets function also includes the newline character in your buffer.
    Remove the newline character from your buffer after the fgets function:
    Code:
    buffer[strlen(buffer)-1] = '\0';
    Try to avoid functions like gets and scanf.
    With these functions you can get a buffer overflow:
    Code:
    int main()
    {
      char buf[10];
      gets(buf);
      return 0;
    }
    If the user enters more than 10 characters the application can crash (access violation) because there is only space for 10 characters.

  6. #6
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    > Try to avoid functions like gets and scanf.

    That's a valid point. Try to use 'fgets(..., stdin)' instead!

  7. #7
    Unregistered
    Guest
    thx for all ur support... i think ive got it now

  8. #8
    Unregistered
    Guest
    this is beginning to be veryvery strange (atleast for my skill)

    this is my full code >>

    #include <stdio.h>

    int main()
    {
    clrscr();
    printf("=[ Dumpfile creator ]=\n\nEnter size in Mb: ");
    FILE *f;
    f=fopen("dump.df","w");
    if(!f)
    {
    printf("Could not open dump.df\n");
    return 1;
    }
    int luku, x, x2, iso;
    int kohta=10000, koko=59000;
    float mgs=1;
    scanf("%d", &x2);
    x = x2 * 59000;
    printf("\n\n\n");
    while(luku<=x)
    {
    fprintf(f, "%d%d\n",luku, iso);
    luku=luku+1;
    iso=iso+luku;
    if(luku == koko)
    {
    clrscr();
    koko=koko+59000;
    printf("Total size: %6.2f Mb", mgs);
    mgs=mgs+1;
    }
    }
    printf("\n\n\nWrite completed to dump.df\n");
    printf("Code by Metael\n");
    fclose(f);
    }


    now ... u told me how to make it ask the file name...

    it should look like this then >>

    #include <stdio.h>

    int main()
    {
    clrscr();
    printf("=[ Dumpfile creator ]=\n\nEnter Filename: ");
    FILE *f;
    char filename[255];
    gets(filename);
    f=fopen(filename,"w");
    if(!f)
    {
    printf("Could not open %s\n", filename);
    return 1;
    }
    int luku, x, x2, iso;
    int kohta=10000, koko=59000;
    float mgs=1;
    printf("\nEnter size in Mb: ");
    scanf("%d", &x2);
    x = x2 * 59000;
    printf("\n\n\n");
    while(luku<=x)
    {
    fprintf(f, "%d%d\n",luku, iso);
    luku=luku+1;
    iso=iso+luku;
    if(luku == koko)
    {
    clrscr();
    koko=koko+59000;
    printf("Total size: %6.2f Mb", mgs);
    mgs=mgs+1;
    }
    }
    printf("\n\n\nWrite completed to %s\n", filename);
    printf("Code by Metael\n");
    fclose(f);
    }

    now when i compile and run this... it stops somewhere around

    scanf("%d", &x2);
    x = x2 * 59000;
    printf("\n\n\n");
    << here >>
    while(luku<=x)
    {

    or something and just creates the file as long as i have enough space on my harddrive, doesnt show the progress as it should and is very irrating because i dont know what to do..

  9. #9
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Some tips:

    - Begin your main with declaring all your variables
    - You are using the var luku in your while loop without initializing it
    - Same story for the var iso
    - int main() -> return something at the end of main
    - gets(filename); -> this can result in a buffer overflow (if the user enters more than 255 characters). Try fgets instead (but watch out for the newline char):
    Code:
    fgets(filename, 255, stdin);
    if(filename[strlen(filename)-1] == '\n')
      filename[strlen(filename)-1] = '\0';
    Cheers,
    Monster

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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