Thread: Opening a file and echoing the contents from it to the file descriptor

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    Unhappy Opening a file and echoing the contents from it to the file descriptor

    I've been stuck on this for hours because I really don't understand file descriptors and how successfully print a file to one.


    Basically, I want to be able to open a file and then echo its contents a character at a time to a socket file descriptor. The code below will hopefully help you to understand, and I've made a start on what I think is correct. I just can't figure out what to include in the while loop, although I think it is through use of char *fgets and int fputs. Any help whatsoever will be greatly appreciated.


    Code:
    void echoFile(int fd, char *filename) {
    
    
        // file pointer
        FILE* fp;
        
        // open 'filename' and assign the pointer
        if((fp = fopen(filename, "r")) == NULL) {
                fprintf(stderr, "The file specified does not exist\n");
        }
        
        //while loop to read each character from 'filename' to the file descriptor
        while(getc(fp) != EOF) {
      
        }
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First you need to know what a file descriptor (sometimes called a file handle) is... CLICK

    Second you need to know what a socket is (and this is where it gets complex)... CLICK

    Basically you need to first open a socket, then get it connected to something... some sort of client that can absorb the data... then stream data from the file to the socket....

    If this is your first week of programming and/or you haven't finished your textbook yet, I strongly suggest you do that first. Sockets programming, especially with Winsock is NOT beginner software.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Sorry I haven't been very clear here.

    EDIT:
    I have already created the socket in the main method, which basically results in:

    Code:
    int connection_sckt;
    connection_sckt = accept(list_s, NULL, NULL);
    
    //and then what I think should be...
    echoFile(connection_sckt, filename)
    //which would cop the contents of filename to the connection_sckt(if this is in fact the correct fd)
    Last edited by purpleturtle_97; 11-29-2011 at 10:21 PM.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Have you tried getting it to work without sending the output to a socket? That might help you get your head sorted a bit.

    That way, at least you'll know that you're echoing the contents properly. So from there, you can begin to attack the other problems one piece at a time.

    Write down your plan of attack.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Thats not a bad idea, thanks for that! I'll get right on that now, but in the meantime am I correct if I were to go along the lines of:

    Code:
    voidechoFile(int fd, char*filename) {
    
        FILE* fp;
        FILE* fr;
    
        //open the file in read mode
        fp = open(filename, "r");
    
        //open the file in write mode
        fr = open(fd, "w");
    
        while(c = getc(fp) != EOF) {
            putc(c, fr)
        }
        
        fclose(fp);
        fclose(fr);
    
    }

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You will want fgetc() and fputc() for lines 12 and 13... getc() and putc() work with keyboard and screen.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by purpleturtle_97 View Post
    Sorry I haven't been very clear here.

    EDIT:
    I have already created the socket in the main method, which basically results in:

    Code:
    int connection_sckt;
    connection_sckt = accept(list_s, NULL, NULL);
    
    //and then what I think should be...
    echoFile(connection_sckt, filename)
    //which would cop the contents of filename to the connection_sckt(if this is in fact the correct fd)
    You should post your whole code and let us look at it.

    Trust me... simply calling accept does not open a socket.
    And using fput() to echo characters to the socket handle does not send data.

    Here's an example of what it takes to open a listening socket in Winsock...
    Code:
    // initialize Client port/return port number
    WORD InitNetwork(WORD Port)
      { WSADATA     wsadata;              // winsock startup
        TCHAR       hn[MAX_HOSTNAME];     // host name
        DWORD       hs = MAX_HOSTNAME;    // host name size
        SOCKADDR    la;                   // local address
        WORD        lp = Port;            // local port
        // Load the Winsock DLL
        if (WSAStartup(MAKEWORD(2,0),&wsadata))
          return 0;
        // initialize local socket
        hSocket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
        if (hSocket == INVALID_SOCKET)
          return 0;
        // initialize localhost IP  
        GetComputerName(hn,&hs);
        if (!GetHostAddr(hn,Port,&la))
          return 0;
        // bind on user designated Port
        if (bind(hSocket,&la,sizeof(SOCKADDR)))
          return 0; 
        // start rx thread
        rxThread = CreateThread(NULL,0,&ReceiveDatagrams,NULL,0,0);
        return lp; }
    This is for UDP connections, TCP is a little different but no less complex.
    Last edited by CommonTater; 11-30-2011 at 12:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File descriptor file position
    By sethjackson in forum Linux Programming
    Replies: 3
    Last Post: 05-28-2008, 10:52 AM
  2. opening a file Descriptor
    By vin_pll in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2007, 05:03 AM
  3. How to read the file descriptor
    By vin_pll in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2007, 01:58 AM
  4. why wrong file descriptor ??
    By alokdotnet in forum C Programming
    Replies: 2
    Last Post: 08-13-2006, 12:59 AM
  5. Echoing a File
    By 0rion in forum C++ Programming
    Replies: 2
    Last Post: 01-27-2006, 11:52 AM

Tags for this Thread