LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-12-2005, 06:20 PM   #1
mdlister
LQ Newbie
 
Registered: Mar 2005
Location: Plymouth UK
Posts: 6

Rep: Reputation: 0
Question 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?
 
Old 03-12-2005, 09:05 PM   #2
Engmar
Member
 
Registered: Oct 2003
Location: The Forest
Distribution: Ubuntu, LFS, DSL
Posts: 127

Rep: Reputation: 16
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.
 
Old 03-13-2005, 05:21 PM   #3
mdlister
LQ Newbie
 
Registered: Mar 2005
Location: Plymouth UK
Posts: 6

Original Poster
Rep: Reputation: 0
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?
 
Old 03-14-2005, 12:25 AM   #4
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
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.
 
Old 03-14-2005, 02:16 AM   #5
nixcraft
Member
 
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379

Rep: Reputation: 30
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
 
Old 03-14-2005, 07:47 AM   #6
mdlister
LQ Newbie
 
Registered: Mar 2005
Location: Plymouth UK
Posts: 6

Original Poster
Rep: Reputation: 0
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);
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading from a txt file into a two dimension array in C kponenation Programming 3 11-26-2005 07:04 PM
c++ writing to a .txt file xemous Programming 8 09-01-2004 10:57 PM
Perl Script Reading a txt file and generate html to be published! kofi Linux - Software 1 09-22-2003 05:12 PM
How to convert a txt file to be a db file in Redhat linux 9? winnie Linux - Newbie 3 06-27-2003 08:33 AM
perl reading pdf,ps,txt j-ray Programming 1 02-04-2003 10:49 AM


All times are GMT -5. The time now is 06:15 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration