very simple, I hope?

This is a discussion on very simple, I hope? within the C Programming forums, part of the General Programming Boards category; Hi, I've just finished coding a database program which saves info to a file currently defined as "mb.dat". The problem ...

  1. #1
    Unregistered
    Guest

    very simple, I hope?

    Hi,

    I've just finished coding a database program which saves info to a file currently defined as "mb.dat".

    The problem is I want the user to give the filename when prompted.

    So how can i save a string variable that I can use in the following code?


    FILE *fp;

    fp = fopen("variable","wb"); /* open file for writing */

    I've had a go at using stringcpy and defining the varible as type CHAR but I'm going wrong somewhere as the compiler just shouts at me!

    I'm a beginner so please go easy. Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char buf[some_size];

    fgets( buf, sizeof(buf), stdin );
    fp = fopen( buf, "wb" );


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

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > fgets( buf, sizeof(buf), stdin );
    Don't forget to remove the \n from the end of the line

    Here's how
    char *p = strchr( buff, '\n' );
    if ( p != NULL ) *p = '\0';

  4. #4
    Unregistered
    Guest
    >Here's how
    >char *p = strchr( buff, '\n' );
    >if ( p != NULL ) *p = '\0';

    Wouldn't this work as well?
    fgets( buf, sizeof(buf) - 1, stdin );

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    >Here's how
    >char *p = strchr( buff, '\n' );
    >if ( p != NULL ) *p = '\0';

    Wouldn't this work as well?
    fgets( buf, sizeof(buf) - 1, stdin );
    No. 'fgets' reads up through a newline character, or until it's read the total number of characters. It always reads up to a newline if there is one in the string before it's buffer is full.

    All you are doing is changing the amount it reads into the buffer, not the fact that it will still read the newline:


    buf[1000];

    fgets( buf, sizeof(buf) -1, stdin ) ... just makes it read up to 999 total characters instead of a thousand. If the strings were something simple, like this:

    "mystring\n"
    "string\n"

    It would still end up reading the newline. If the strings were 2000 characters in length, without a newline, it'd only read 999 of the characters and stop.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple simple graphics
    By triplem in forum C Programming
    Replies: 2
    Last Post: 05-19-2003, 02:52 AM
  2. simple graphic programing
    By mattyb in forum Game Programming
    Replies: 10
    Last Post: 12-30-2002, 08:56 PM
  3. File Input and Output, simple.
    By Vber in forum C Programming
    Replies: 5
    Last Post: 11-17-2002, 01:57 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM
  5. Help me with these simple programs
    By Help me in forum C Programming
    Replies: 4
    Last Post: 11-08-2001, 09:38 AM

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