Thread: File i/o filename its extension

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    25

    File i/o filename its extension

    Hi,

    Is there way to save the whole filename and its extension. Right i now i have a client/server application where client sends a file(for example test.jpg) server save the data of that file with specified filename "copy-client". It dose not saves the filename and its extension.

    How do i save the filename and its extension?

    Code:
    FILE *fp = fopen("copy-client", "w" );
          fwrite(buffer, sizeof(buffer), current, fp);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So change "copy-client" to "copy-client.jpg".


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

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    25
    I know this works, but is not the solution. Fore example if i send next time a "test.txt" file, server must save filename and the extension of the data as it is, in this case as "test.txt" . I dont need to change by hand everytime.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So put it into a buffer and use that for the name of the file. What's the problem?


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

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    25
    I tried your way by putting it in a buffer.
    Code:
     // create and init a char buffer
        char *buffer = malloc(BUFFSIZE);
        buffer[0] = '\0';
        
        
        int stop_received = 0;
        int nr_bytes_recv = 0;
        
        while (! stop_received) {
            // receive data in buffer
            nr_bytes_recv = recv(s, buffer, BUFFSIZE, 0);
            
            if (nr_bytes_recv > BUFFSIZE) {
                perror("Buffer too small");
                exit(EXIT_FAILURE);
            }        
            buffer[nr_bytes_recv]= '\0'; // close string
            printf("Server received : %s", buffer);
            
    if(strncmp(buffer, "upload", 6) == 0){
                // echo message
                rv = send(s, buffer, nr_bytes_recv,0);
                if (rv < 0) {
                    perror("Error sending");
                    exit(EXIT_FAILURE);
                }
                do {
                // receive data in buffer
                    nr_bytes_recv = recv(s, buffer, (BUFFSIZE-current), 0);
                    if(nr_bytes_recv > 0)
                        current += nr_bytes_recv;
                } while (nr_bytes_recv > 0);
    
                FILE *fp = fopen(buffer, "w" );
                fwrite(buffer, sizeof(buffer), current, fp);
                fclose(fp);
                close(s);
                stop_received = 1;
    The results: it makes a file "upload" with the name of string commando which clients sends. As you can see in the code i use prints function to print out the buffer, this is the output of the buffer
    Server received : upload
    test.txt
    I think the problem is the client sends 3 items in a buffer, the string "upload", filedata and filename. The fopen function takes the first item from the buffer instead of filename.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Alix View Post
    I think the problem is the client sends 3 items in a buffer, the string "upload", filedata and filename. The fopen function takes the first item from the buffer instead of filename.
    So take the right thing out of your buffer.

    I don't know why you are making this so hard. You said you are getting the name from some place, right? Take that name when you get it, and stick it in a variable some place, and use that for fopen:
    Code:
    #include<stdio.h>
    int main( void )
    {
        char buf[ BUFSIZ ] = { 0 };
        FILE *fp = NULL;
    
        printf( "Enter a file name: " );
        fflush( stdout );
        scanf( "%s", buf );
    
        printf( "You entered: \'%s\'. Opening...\n", buf );
        fp = fopen( buf, "r" );
        if( fp == NULL )
            printf( "Your file didn't open.\n" );
        else
            fclose( fp );
    
        return 0;
    }
    Looks about right. So take the name that you said you had in the first post, and stick it in a buffer, and slap the extension on the end (since you said you're getting it some place too), and then open the file.

    If all that is too confusing:
    1. Format your string.
    2. Use it to open your file.


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

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Alix View Post
    I know this works, but is not the solution. Fore example if i send next time a "test.txt" file, server must save filename and the extension of the data as it is, in this case as "test.txt" . I dont need to change by hand everytime.

    Seriously?

    Code:
    char filename[256];
    
    // put client's filename in filename[]
    
    
    fp = fopen(filename,"w");
    Seems pretty obvious...

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    25
    Quote Originally Posted by CommonTater View Post
    Seriously?

    Code:
    char filename[256];
    
    // put client's filename in filename[]
    
    
    fp = fopen(filename,"w");
    Seems pretty obvious...
    I understood that part, i know i have to do the folowing:

    Code:
    char filename[10];
    scanf("%s", filename);
    FILE *fp = fopen(filename, "w" );

    But that is not my problem, my problem is how do i extract the second string from the buffer. The second string is just the name of the file (test.txt), you can see that on my previous post when i print the buffer. I have a buffer
    Code:
    char *buffer = malloc(BUFFSIZE);
    . that buffer i use for my protocol en for the data to be saved. The question is how do i specifie the filename from that buffer. How do i link those two with each other? I just dont see it.

    Code:
    scanf("%s %s", &buffer);
    Last edited by Alix; 01-17-2012 at 12:49 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you know the format of the data you are receiving, just pick it apart as you get it. If I give you a string, and ask you to take the second word from it, how would you do it?

    "take second word"

    If you can't understand that, then you can't solve this problem, regardless of where your data comes from.


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

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    25
    Quote Originally Posted by quzah View Post
    If you know the format of the data you are receiving, just pick it apart as you get it. If I give you a string, and ask you to take the second word from it, how would you do it?

    "take second word"

    Quzah.
    Code:
    char *string;
    
    string= (char *) malloc (25);
    scanf("%5s ", string);
    printf(string)
    like that?


    But then how do i know wich string is the name of the file that is stored in the buffer?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Alix View Post
    like that?
    No. Not like that. How does that get you the word 'second' out of that? If you can't walk through the processes on paper, how can you expect to do it in C? Write out how to find the second word in a sentence. If you can't, you can't possibly understand how to program this.


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

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    25
    In sentence:
    "take second word"
    1) Read the first string,
    2) Stop at the empty space,
    3) Read the second string
    4) Stop at the empty space
    5) Select the string the last string

  13. #13
    Registered User
    Join Date
    Jan 2012
    Posts
    25
    I have almost working , but only the problem is i specifie the wrong string. What it dose it takes the first string of the content of the file as the name of the file and stores it. Instead of using scanf i decided to use sscanf . like i sayt in my buffer there are 3 information are stored

    First: string "upload"
    Second: the name of the file
    Third: the content of the data

    This is my code:

    Code:
    char string1[10];
    sscanf(buffer,"%s %s %s",string1);
     printf("%s ->", string1);
    FILE *fp = fopen(string1, "w" );
    i tested with simple .txt with one word inside the file "serverrrr". i just test with array of [10]. Results with this case it makes a file, only it names the file by taking the contents of the file en closing with extension .txt .

    serverrrr.txt
    Same question, how do i select the correct string from the buffer? how do i specify that?

  14. #14
    Registered User
    Join Date
    Jan 2012
    Posts
    25
    Ok never mind, i got it working. sscanf was the solution and the problem of the previus post is also solved. The solution was dat the call of sscanf was in wrong place.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 01-11-2012, 04:56 AM
  2. Changing filename extension
    By slippy in forum C Programming
    Replies: 3
    Last Post: 05-03-2007, 11:04 AM
  3. Getting a File Extension?
    By Nebbuchadnezzar in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2006, 12:45 PM
  4. Replies: 6
    Last Post: 04-11-2006, 04:52 PM
  5. allow another file extension??
    By C+noob in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-03-2005, 08:52 PM