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-15-2004, 10:42 AM   #1
Claus
Member
 
Registered: Jul 2003
Location: Santiago de Chile
Distribution: Debian testing/unstable
Posts: 74

Rep: Reputation: 15
C: Best way to read an entire text file and putting it in a *char.


Hi

I'm developing a GTK+ Text Editor to learn something more about this toolkit.

I'd like to know a good way to read a text file and store it in a *char. At this moment i'm using for this the following piece of code:

Code:
		file = fopen (argv[1], "r");
		string = g_string_new (NULL);
		
		while (!feof (file)){	
			fgets (text, 1000, file);
			g_string_append (string, text);
		}
		fclose (file);
		buffer = string->str;
		g_string_free (string, FALSE);
But with some text files the program crashes.

Is there any other way to read an entire file or line by line safety?

Thank you.

entire program:

Code:
 
#include <stdio.h>
#include <gtk/gtk.h>

GtkWidget *
create_main (char *text)
{
   	GtkWidget *window;
	GtkWidget *button;
	GtkWidget *vbox;
	GtkWidget *textview;
	GtkTextBuffer *buffer;

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);
	gtk_window_set_title ( GTK_WINDOW (window), "CSV's Text Editor");
	g_signal_connect (G_OBJECT (window), "destroy",
			  G_CALLBACK (gtk_main_quit), NULL);
	
	textview = gtk_text_view_new ();
	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));
	gtk_text_buffer_set_text (buffer, text, -1); 
	
	vbox = gtk_vbox_new (TRUE, 0);
	gtk_container_add (GTK_CONTAINER (window), vbox);
	gtk_box_pack_start (GTK_BOX (vbox), textview, TRUE, TRUE, 0);
	gtk_widget_show_all (vbox);
		
	return window;
}

int main(int  argc,
	 char *argv[])
{
	GtkWidget *window;
	FILE *file;
	char *text = (char *) malloc (1000*sizeof (char));
	char *buffer;
	GString *string;

	gtk_init (&argc, &argv);

	if (argc > 1){
		file = fopen (argv[1], "r");
		string = g_string_new (NULL);
		
		while (!feof (file)){	
			fgets (text, 1000, file);
			g_string_append (string, text);
		}
		fclose (file);
		buffer = string->str;
		g_string_free (string, FALSE);
	}
	else 
		buffer = NULL;

	window = create_main (buffer);

	gtk_widget_show (window);

		
	gtk_main ();
    
	return 0;
}
 
Old 03-15-2004, 11:01 AM   #2
Claus
Member
 
Registered: Jul 2003
Location: Santiago de Chile
Distribution: Debian testing/unstable
Posts: 74

Original Poster
Rep: Reputation: 15
by the way, to compile it use:

gcc -o edit `pkg-config --cflags --libs gtk+-2.0` edit.c
 
Old 03-15-2004, 11:13 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,311

Rep: Reputation: 175Reputation: 175
Nothing obvious.
do you know where it crashes?

do you get a core file?
have you gdb'd the core file to find the crash?



billy


billy
 
Old 03-15-2004, 11:16 AM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,539

Rep: Reputation: 149Reputation: 149
It's not a good idea to read the whole file. Imagine what can happen if your file is 100MB. I'd rather make a dynamic buffer (with defined max size and procedures what to do when it's reached) and read the amount of data needed at the moment.
 
Old 03-15-2004, 11:18 AM   #5
hiteshmaisheri
Member
 
Registered: Nov 2003
Location: Kerala, India
Distribution: Red Hat, Knoppix, Mandrake, FreeBSD
Posts: 231

Rep: Reputation: 30
thanks for the information....
 
Old 03-15-2004, 11:22 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,311

Rep: Reputation: 175Reputation: 175
I should imagine maybe that's what Gstring does?
not sure though!

billy
 
Old 03-15-2004, 11:22 AM   #7
Claus
Member
 
Registered: Jul 2003
Location: Santiago de Chile
Distribution: Debian testing/unstable
Posts: 74

Original Poster
Rep: Reputation: 15
I dont care too much about the crash, i'm interesed in different ways to read strings from files.

Thank you all..
 
Old 03-15-2004, 11:30 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,311

Rep: Reputation: 175Reputation: 175
Well, how about not doing it?
I had to do a read and substitute file parser.
I didn't bother with that, keep it simple.

Open the file and copy it to another.
then re-open the copy again for rw and using, ftell(), fseek(), fgetc and fputc(), you can navigate in the
copied file, change it and let the standard libraries worry about the buffering and such.

just another approach maybe?


billy
 
Old 03-15-2004, 02:22 PM   #9
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
I use the return value from fgets() instead of feof().

Code:
char *read_file(FILE *fp)
{
  char *str = malloc(4096), *s = str;
  int len = 0;

  while(fgets(s, 4096, fp))
  {
    len += strlen(s);
    str = realloc(str, len+4096);
    s = str+len;
  }
}
 
  


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
BASH: Read entire file line in for loop clinton Programming 16 04-18-2013 01:06 PM
need something that can read a Microsoft Works text file joshknape Linux - Software 1 10-30-2005 12:45 AM
Read a char from a file (PERL) linuxlover1 Programming 4 01-09-2005 10:10 AM
Read in an Octal number from a text file using C++ pjordan Programming 2 11-18-2004 04:03 PM
Can't read entire file from CD under RH7.1 t0dd Linux - General 2 01-28-2002 01:40 PM


All times are GMT -5. The time now is 05:50 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration