LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 07-28-2007, 11:53 AM   #1
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Rep: Reputation: 30
Simple C file I/O question


Hi!

I am having trouble opening a file and then reading its content and in the end displaying it. Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	FILE *fp;
	fp = fopen("/boot/grub/menu.lst", "r");
	char *line;
	line = malloc(128);
	int i;
	if(fp == NULL)
	{
		printf("Could not open the file!\n");
	}else
	{
		printf("File opened for input");
		while(!feof(fp))
		{
			fgets(line, 128, fp);
			for(i = 0; i < 128; i++ )
				printf("%c", line[i]);
		}
	}
	fclose(fp);
	return 0;
}
The output is a complete mess:
Quote:
itrd /boot/initrd.img-2.6.20-15-generic
=UUID=2b154dc2-73a3-4df2-817a-90e112bc04cd ro single
lash
title Ubuntu, memtest86+
6.20-15-generic
=UUID=2b154dc2-73a3-4df2-817a-90e112bc04cd ro single
lash
root (hd0,0)
memtest86+
6.20-15-generic
=UUID=2b154dc2-73a3-4df2-817a-90e112bc04cd ro single
lash
kernel /boot/memtest86+.bin
0-15-generic
=UUID=2b154dc2-73a3-4df2-817a-90e112bc04cd ro single
lash
quiet
/boot/memtest86+.bin
0-15-generic
=UUID=2b154dc2-73a3-4df2-817a-90e112bc04cd ro single
lash
And this is just a part of the output. As you can see, parts of the things are repeated. How could I prevent this? And what could go wrong?

Thanx:
Dan
 
Old 07-28-2007, 12:06 PM   #2
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Why are you printfing the contents of your buffer one char at at time? More importantly, why loop from 0 to 128 when lines are usually not exactly 128 characters long? That's probably why you're printing so much garbage.
 
Old 07-28-2007, 12:13 PM   #3
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
Yes... I noticed that after NULLing the value of line[i] the garbage is not gonne be printed out. The loop is 128 because a line is not gonna be longer than 128chars/bytes. I thought that this must be enough.
Is there a more dynamic way to allocate the buffer or something? How can I print out the entire line at once?
 
Old 07-28-2007, 12:15 PM   #4
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
To print the entire line at once, use:

Code:
printf("%s\n", line);
or

Code:
puts(line);
 
Old 07-28-2007, 12:18 PM   #5
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
printf("%s", line);
This one worked like a charm. Awesome... I told you it was easy
Thanx
 
Old 07-28-2007, 12:30 PM   #6
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
back to the more dynamic memory handling thing that I mentioned above.
Is there any other way to handle the memory more dynamically? To use only that is needed?
 
Old 07-28-2007, 12:53 PM   #7
binutils
Member
 
Registered: Feb 2007
Posts: 59

Rep: Reputation: 15
Code:
	char *line;
	line = malloc(128);
above code is

Code:
#define MAXINPUT 128
char line[MAXINPUT]={0};
try to use malloc() between 3 and 4
Code:
1	while(!feof(fp))
2	{
3		fgets(line, 128, fp);
4		for(i = 0; i < 128; i++ )
5			printf("%c", line[i]);
6	}
 
Old 07-28-2007, 12:59 PM   #8
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
I thought that I had to predefine all the memory reservations in the beginning of each function. I don't know why. That works too. Thanks
 
  


Reply



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
simple tar question can not find answer in man file davimint Linux - General 3 05-22-2007 01:03 AM
Ubuntu Fluxbox simple question, simple answer? generallimptoes Linux - Software 3 09-26-2005 02:03 PM
Simple question - how to edit config file in Suse 9.1? rollo Linux - Distributions 3 07-18-2004 08:47 AM
Simple Question - how to modify file attributes? xanas3712 Linux - Newbie 12 04-30-2004 07:03 PM
Installing Programs - A simple question from my simple mind jmp875 Linux - Newbie 6 02-18-2004 09:03 PM

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

All times are GMT -5. The time now is 05:41 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