LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-22-2008, 07:22 PM   #1
darkangel29
Member
 
Registered: Nov 2004
Location: Puerto Rico
Distribution: Ubuntu 10.04
Posts: 121

Rep: Reputation: 15
How to save to a file in C?


I want to save everything this function prints on screen to a file. I'm losing my mind, I have been trying but no luck so far. Can anybody help to accomplish this? Thanks.
Also this is in C.

Code:
static void print_ip_header(struct iphdr *ip){
	struct in_addr    saddr, daddr;
	char            sip[24], dip[24];

	saddr.s_addr = ip->saddr;
	strcpy(sip, inet_ntoa(saddr));
	daddr.s_addr = ip->daddr;
	strcpy(dip, inet_ntoa(daddr));
	printf("IP %s -> %s \n IP  ver.%d header-length.%d", sip, dip, ip->version, ip->ihl);
	printf("IP  ver.%d header-length.%d TOS.%04X total-len.%d TTL.%d \n",ip->tos, ntohs(ip->tot_len), ip->ttl);
	printf("id.%d offset.%d checksum.%d\n ", ntohs(ip->id), ntohs(ip->frag_off), ntohs(ip->check));
}
 
Old 12-22-2008, 07:29 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Ever heard of fprintf ?

What about 'tee' and redirection on command line:

Code:
your_program ARGS | tee > screen_output.txt
?
 
Old 12-22-2008, 08:09 PM   #3
darkangel29
Member
 
Registered: Nov 2004
Location: Puerto Rico
Distribution: Ubuntu 10.04
Posts: 121

Original Poster
Rep: Reputation: 15
Thanks!!!
 
Old 12-25-2008, 01:12 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I thought you wanted to do this in C...

Code:
static void print_ip_header(struct iphdr *ip){
	struct in_addr    saddr, daddr;
	char            sip[24], dip[24];
        FILE * logfile;
        logfile = fopen( "logfile.txt", "w" );
        if( NULL == logfile ){
               perror( "can't open logfile" );
               return;
        }
	saddr.s_addr = ip->saddr;
	strcpy(sip, inet_ntoa(saddr));
	daddr.s_addr = ip->daddr;
	strcpy(dip, inet_ntoa(daddr));
	printf("IP %s -> %s \n IP  ver.%d header-length.%d", sip, dip, ip->version, ip->ihl);
	printf("IP  ver.%d header-length.%d TOS.%04X total-len.%d TTL.%d \n",ip->tos, ntohs(ip->tot_len), ip->ttl);
	printf("id.%d offset.%d checksum.%d\n ", ntohs(ip->id), ntohs(ip->frag_off), ntohs(ip->check));

	fprintf(logfile, "IP %s -> %s \n IP  ver.%d header-length.%d", sip, dip, ip->version, ip->ihl);
	fprintf(logfile, "IP  ver.%d header-length.%d TOS.%04X total-len.%d TTL.%d \n",ip->tos, ntohs(ip->tot_len), ip->ttl);
	fprintf(logfile, "id.%d offset.%d checksum.%d\n ", ntohs(ip->id), ntohs(ip->frag_off), ntohs(ip->check));
        fclose( logfile );
}
--- rod.
 
Old 12-25-2008, 09:36 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by theNbomr View Post
I thought you wanted to do this in C...

Code:
static void print_ip_header(struct iphdr *ip){
	struct in_addr    saddr, daddr;
	char            sip[24], dip[24];
        FILE * logfile;
        logfile = fopen( "logfile.txt", "w" );
        if( NULL == logfile ){
               perror( "can't open logfile" );
               return;
        }
	saddr.s_addr = ip->saddr;
	strcpy(sip, inet_ntoa(saddr));
	daddr.s_addr = ip->daddr;
	strcpy(dip, inet_ntoa(daddr));
	printf("IP %s -> %s \n IP  ver.%d header-length.%d", sip, dip, ip->version, ip->ihl);
	printf("IP  ver.%d header-length.%d TOS.%04X total-len.%d TTL.%d \n",ip->tos, ntohs(ip->tot_len), ip->ttl);
	printf("id.%d offset.%d checksum.%d\n ", ntohs(ip->id), ntohs(ip->frag_off), ntohs(ip->check));

	fprintf(logfile, "IP %s -> %s \n IP  ver.%d header-length.%d", sip, dip, ip->version, ip->ihl);
	fprintf(logfile, "IP  ver.%d header-length.%d TOS.%04X total-len.%d TTL.%d \n",ip->tos, ntohs(ip->tot_len), ip->ttl);
	fprintf(logfile, "id.%d offset.%d checksum.%d\n ", ntohs(ip->id), ntohs(ip->frag_off), ntohs(ip->check));
        fclose( logfile );
}
--- rod.
Do you really think to do copy-paste of each output string is a good idea ?

What about the concept of "single point of change" ?

What about defining, say, a macro for each string to be printed - nowadays compilers know well to allocate memory only once.

What about writing a function (maybe one already exists) which would wrap 'fprintf' and would have a pointer to an array of open file descriptors and would write the same stuff to all of them ? Or maybe a varargs macro.
 
  


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
How to modify a field in few lines in a file and save the new file - in Perl rounak94 Programming 1 10-02-2008 07:43 PM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
Not able to save file shadow11 Linux - Newbie 4 10-21-2004 08:15 PM
where should I save this file??? bruno buys Linux - Software 2 08-17-2004 06:54 PM
Mozilla 1.7 - No File Save / File Open dialogs on Slackware 10.0 jayseye Slackware 8 08-10-2004 05:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:33 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration