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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-15-2004, 09:42 AM
|
#1
|
|
Member
Registered: Jul 2003
Location: Santiago de Chile
Distribution: Debian testing/unstable
Posts: 74
Rep:
|
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;
}
|
|
|
|
03-15-2004, 10:01 AM
|
#2
|
|
Member
Registered: Jul 2003
Location: Santiago de Chile
Distribution: Debian testing/unstable
Posts: 74
Original Poster
Rep:
|
by the way, to compile it use:
gcc -o edit `pkg-config --cflags --libs gtk+-2.0` edit.c
|
|
|
|
03-15-2004, 10:13 AM
|
#3
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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
|
|
|
|
03-15-2004, 10:16 AM
|
#4
|
|
Moderator
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,471
Rep: 
|
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.
|
|
|
|
03-15-2004, 10:18 AM
|
#5
|
|
Member
Registered: Nov 2003
Location: Kerala, India
Distribution: Red Hat, Knoppix, Mandrake, FreeBSD
Posts: 231
Rep:
|
thanks for the information....
|
|
|
|
03-15-2004, 10:22 AM
|
#6
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
I should imagine maybe that's what Gstring does?
not sure though!
billy
|
|
|
|
03-15-2004, 10:22 AM
|
#7
|
|
Member
Registered: Jul 2003
Location: Santiago de Chile
Distribution: Debian testing/unstable
Posts: 74
Original Poster
Rep:
|
I dont care too much about the crash, i'm interesed in different ways to read strings from files.
Thank you all..
|
|
|
|
03-15-2004, 10:30 AM
|
#8
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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
|
|
|
|
03-15-2004, 01:22 PM
|
#9
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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;
}
}
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:33 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|