C program to write a string to a file.

Posted: May 23, 2016. At: 5:05 PM. This was 2 years ago. Post ID: 9157
Page permalink.
WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters. These cookies expire two weeks after they are set.

I think I may have posted this before, but this is a little program I am working on that writes a text string to a file. I have got it to work perfectly and it compiles without errors using gcc -Wall.

/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
 
/********************************************************************
* Description:
* Author:  <shoggoth>
* Created at: Wed Jan 19 13:06:21 EST 2011
* Computer: myhost
* System: Linux 2.6.33-ARCH on x86_64
*
* Copyright (c) 2011   All rights reserved.
*
********************************************************************/
 
#include <stdio.h>
#include <time.h>
 
#define format "At this time: %H:%M:%S"
#define text "OP is a Troll."
 
int lineofstars (void) {
	int x = 0;
	while (x < 64) {
		printf("*");
		x++;
		if (x == 31) {
			printf("<|>");
		} else if (x == 64) {
			printf("\n-\n");
		}
	}
	return 0;
}
 
int main (int argc, char** argv) {
 
	lineofstars();
 
	char *File;
	char String[60];
	struct tm *ptr;
	time_t tm;
	char length[60];
 
	tm = time(NULL);
	ptr = localtime(&tm);
	strftime(length, 100, format, ptr);
 
	File = "log.txt";
	snprintf(String, 100, "%s, %s\n", length, text);
 
	FILE *f;
	f = fopen (File, "a+");
 
	if (!f) {
		printf("Sorry, I cannot open the file %s.\n", File);
		return 0;
	}
 
	fprintf(f, String);
 
	fflush(stdout);
	fclose(f);
 
	return 0;
}

And this is the ~/.xsession file I am currenty using when I want a minimal desktop. Larswm is the fastest desktop available for Linux and very minimal.

#!/bin/sh
 
#
# Copyright (c) 2004 Lars Bernhardsson, see README for licence details
#
# sample.xsession - Login script for larswm
#
 
# Clear root window settings and set background color
xsetroot && xsetroot -solid DarkSlateGrey
 
# 2nd screen
# xsetroot -display :0.1 && xsetroot -display :0.1 -solid lightgray
 
# Start a couple of tools
xload -geometry 96x48-0-0 &
xbiff -geometry 48x48-100-0 &
oclock -geometry 48x48-152-0 &
 
# Start a background job that feeds date/time to larswm
larsclock &
 
# Start wm.
exec larswm
[Ackley] took another look at my hat . . . "Up home we wear a hat like that to shoot deer in, for
Chrissake", he said. "That's a deer shooting hat."
Like hell it is. I took it off and looked at it. I sort of closed one eye, like I was taking aim at it.
"This is a people shooting hat," I said. "I shoot people in this hat."
	--The Catcher in the Rye, J.D Salinger. 1951.

No comments have been made. Use this form to start the conversation :)

Leave a Reply