LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Reading a txt file in terminal over a network (https://www.linuxquestions.org/questions/programming-9/reading-a-txt-file-in-terminal-over-a-network-300910/)

mdlister 03-12-2005 06:20 PM

Reading a txt file in terminal over a network
 
Hi, very new to linux and am trying to create a program that connects to a remote text file from another computer and displays the txt file on the screen.

I'm trying to make it so it misses out all the header stuff when its displayed and i know there are 2 new lines before the text starts so i was told that i could do a search for the 2 new lines then display all txt after that?

Anyone got any ideas on what to do or where to start?

Engmar 03-12-2005 09:05 PM

One idea - log into the remote system using ssh. Once there you can use "less" to view the file. less has a _bunch_ of command line options, including searching for patterns and displaying all text after them, and moving down a specific number of lines and displaying everything after that. Read the man page ("man less") for a full explanation. In any case, if you are leaving this ssh session open you can just use the "R" command to refresh your view of the file using your original command.

Hope this helps you off in the right direction.

mdlister 03-13-2005 05:21 PM

apparently less is used to view text files.. totally different from programming something to download a text file, you have to open a socket, send the HTTP command to get the file, and display it.

Anyone else know how to do this?

The_Nerd 03-14-2005 12:25 AM

Yes:

(1): Use a network file system.
(2): Do what you said, open a socket and request it via HTTP
(3): Use wget, store it in a temporary file, and display it, like so:

Code:

int main(int argc, char **argv)
{
    FILE *Handle;
    system("wget http://www.myserver.com/textfile.txt");

    Handle = fopen("textfile.txt", "rb");
    /* Skip first two lines */
    while(!feof(Handle)) printf("%c", fget(Handle));
    fclose(Handle);
}

That would work.

Or:
(4): Write your own protocol, use sockets, and have your own daemon running on the server.

nixcraft 03-14-2005 02:16 AM

Try
Code:

ssh user@ssh.server.com cat /path/to/file > /tmp/local.txt
/tmp/local.txt will have copy of remote /path/to/file. If you are using it in script make sure you use ssh-keys to avoid password prompt just see howto do this http://www.cyberciti.biz/nixcraft/vi...ntication.html

mdlister 03-14-2005 07:47 AM

hi, today i managed to finish the code i was doing, just wanted to say thanx! ended up using the write and get command.

write(our_socket, "GET /~pjb/tmp/cnet221file.txt\n", 30);
recv(our_socket, buffer, 1000, 0);
printf("%s", buffer);


All times are GMT -5. The time now is 02:13 PM.