Writing into a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gnanapoongothai
    New Member
    • Jun 2007
    • 62

    Writing into a file

    hi,
    i am doing socket programming , and once socket is connected getting data from client and wrting into the file.
    the file is created but nothing is in it? whats up ?
    code:
    [CODE=cpp] WORD wVersionRequest ed;
    int err;

    WSADATA wsaData;
    SOCKET m_socket;
    SOCKET AcceptSocket;
    SOCKADDR_IN service;
    HANDLE hFile;
    DWORD dwBytesRead, dwBytesWritten;

    #define MY_MESSAGE_NOTI FICATION 1048 //Custom notification message

    int bytesSent;
    int bytesRecv = SOCKET_ERROR;
    char sendbuf[32] = "Sever sending data";
    char recvbuf[32] = "";


    //sockaddr_in server;

    int iResult = WSAStartup( 0X0202, &wsaData );
    if ( iResult != NO_ERROR )
    {
    printf("Error at WSAStartup()\n" );
    return(0);
    }



    m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if ( m_socket == INVALID_SOCKET )
    {
    printf( "Error at socket(): %ld\n", WSAGetLastError () );
    WSACleanup();
    return(0);
    }

    service.sin_fam ily = AF_INET;
    service.sin_add r.s_addr = inet_addr( "127.0.0.1" );
    service.sin_por t = htons(27015);

    if ( bind( m_socket, (SOCKADDR *)&service, sizeof(service) ) == SOCKET_ERROR )
    {
    printf( "bind() failed.\n %ld\n", WSAGetLastError () );
    closesocket(m_s ocket);
    return(0);
    }
    if ( listen( m_socket, 1 ) == SOCKET_ERROR )
    printf( "Error listening on socket. %ld \n",WSAGetLastE rror() );

    //creating a object of socket for accepting the connection


    printf( "Waiting for a client to connect...\n" );
    while (1)
    {
    AcceptSocket = SOCKET_ERROR;
    while ( AcceptSocket == SOCKET_ERROR )
    {
    AcceptSocket = accept( m_socket, NULL, NULL );
    }
    printf( "Client Connected.\n");
    m_socket = AcceptSocket;
    break;
    }



    bytesRecv = recv( m_socket, recvbuf, 32, 0 );
    printf( "Bytes Recv: %ld\n", bytesRecv );
    bytesSent = send( m_socket, sendbuf, strlen(sendbuf) , 0 );
    printf( "Bytes Sent: %ld\n", bytesSent );


    hFile = CreateFile(TEXT ("myfile.xls "), // file to create
    GENERIC_WRITE | GENERIC_READ ,// open for writing &reading
    0, // do not share
    FILE_SHARE_WRIT E, // default security
    OPEN_EXISTING, // overwrite existing
    FILE_ATTRIBUTE_ NORMAL | // normal file
    FILE_FLAG_OVERL APPED, // asynchronous I/O
    NULL); // no attr. template

    if (hFile == INVALID_HANDLE_ VALUE)
    {
    printf("Could not open file (error )%ld\n", GetLastError()) ;

    }


    do {
    WriteFile(hFile , recvbuf, 512,
    &dwBytesWritten , NULL);
    }while(dwBytesW ritten == 32);

    GetLastError();

    CloseHandle(hFi le);
    GetLastError();
    return(0);[/CODE]
    Last edited by r035198x; Jul 11 '07, 10:02 AM. Reason: added code tags
  • kky2k
    New Member
    • May 2007
    • 34

    #2
    are u sure the file is created?..if yes then where it is created i mean which path...

    Comment

    • gnanapoongothai
      New Member
      • Jun 2007
      • 62

      #3
      the file is being created in the folder where the project codes (.c & .cpp) files are there in visual studio projects.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by gnanapoongothai
        do {
        WriteFile(hFile , recvbuf, 512,
        &dwBytesWritten , NULL);
        }while(dwBytesW ritten == 32);
        Do I read this correctly? You ask to write 512 bytes and then check that 32 bytes are written? It looks like dwBytesWritten won't be 32 after the first write so you drop out of the loop.

        Comment

        • gnanapoongothai
          New Member
          • Jun 2007
          • 62

          #5
          Do i need to allocate memory before writing into the file and is it that the no of bytes written should be in multiples of 512. And the file creation parameters are they fine? will they work for writing once to a file and closing to it.

          Comment

          • gnanapoongothai
            New Member
            • Jun 2007
            • 62

            #6
            What the moderator said was true and also check up with the "dwflagsandatrr ibutes" parameter in the create file. its only File_Atrribute_ normal. And also the do while loop make it write the no of times. So do while loop is also not needed. Else is working fine.
            thanks for the help.

            Comment

            Working...