Reading a file

D

deadpickle

I'm very new to C and am trying to read a file that contains integers
and character. This file has one line but that line is 800000+
columns. Very large. When I use the code below I get the error:
segmentation fault
any ideas?

gcc -Wall -g goo_0.01.c -o /home/deadpickle/Desktop/goo_0.01 `pkg-
config --cflags gtk+-2.0 goocanvas` `pkg-config --libs gtk+-2.0
goocanvas`

#include <gtk/gtk.h>

#include <goocanvas.h>

#include <stdio.h>



static gboolean delete_event(GtkWidget *widget,GdkEvent
*event,gpointer data)

{

g_print ("delete event occurred\n");

return FALSE;

}



static void destroy( GtkWidget *widget,gpointer data )

{

gtk_main_quit ();

}



int main( int argc,char *argv[] )

{

//~ Type Declaration

GtkWidget *window;

GtkWidget *scwin;

GtkWidget *vbox;

GtkWidget *hbox;

GtkObject *adj;

GtkWidget *zoom_spin;

GtkWidget *zoom_label;

GtkWidget *canvas;

FILE *infile;

int i;

char numbers[1];



//~ gtk_init

gtk_init (&argc, &argv);



//~ New Declarations

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

scwin = gtk_scrolled_window_new(NULL,NULL);

vbox = gtk_vbox_new(FALSE,5);

hbox = gtk_hbox_new(FALSE,5);

adj = gtk_adjustment_new(100, 10, 200, 10, 0, 0);

zoom_spin = gtk_spin_button_new(GTK_ADJUSTMENT(adj),0,0);

zoom_label = gtk_label_new("Zoom: ");

canvas = goo_canvas_new();



//~ Signals

g_signal_connect (G_OBJECT (window), "delete_event",G_CALLBACK
(delete_event), NULL);

g_signal_connect (G_OBJECT (window), "destroy",G_CALLBACK (destroy),
NULL);



//~ Properties

gtk_container_set_border_width(GTK_CONTAINER(window),10);

gtk_window_set_default_size(GTK_WINDOW(window),800,600);

gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);

gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
(scwin),GTK_POLICY_ALWAYS,GTK_POLICY_ALWAYS);

g_object_set(G_OBJECT(canvas),"background-color","black",NULL);



//~ Adding and Packing

gtk_container_add(GTK_CONTAINER(window),vbox);

gtk_container_add(GTK_CONTAINER(scwin),canvas);

gtk_box_pack_start(GTK_BOX(hbox),zoom_label,FALSE,FALSE,1);

gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,FALSE,1);

gtk_box_pack_start_defaults(GTK_BOX(vbox),scwin);

gtk_box_pack_start(GTK_BOX(hbox),zoom_spin,FALSE,FALSE,1);



//~ Read shapefile file

infile = fopen("county_colorado.gis","r");

while(!feof(infile)) {

fscanf(infile, "%c", &numbers);

i++;

printf("%c", numbers);

}

fclose(infile);



gtk_widget_show_all(window);



gtk_main();



return 0;

}
 
D

deadpickle

Made these changes:
#include <stdio.h>



int main( int argc,char *argv[] )

{

//~ Type Declaration

FILE *infile;

int i;

char shapefile_read[1];



//~ Read shapefile file

infile = fopen("county_colorado.gis","r");

//~ Simplify by removing for and initializing i

for(i=0 ; i<1 ; i++) {

fscanf(infile, "%s", shapefile_read);

printf("%s", shapefile_read);

}

fclose(infile);



return 0;

}

The string of the file is something like this:
M 2196.693 3021.689 L 2196.709 .....

I get these errors:
goo_0.01.c:67: warning: format ‘%s’ expects type ‘char *’, but
argument 3 has type ‘int’
goo_0.01.c:68: warning: format ‘%s’ expects type ‘char *’, but
argument 2 has type ‘int’

Do I want this to be a pointer or what?
 
G

Guest

the main problem is we've no little idea about what you are trying to
do.
Made these changes:

your layout is bizzare
#include <stdio.h>

int main( int   argc,char *argv[] )

{
        //~ Type Declaration

this is a pretty useless comment

        FILE *infile;
        int i;

        char shapefile_read[1];

        //~ Read shapefile file
        infile = fopen("county_colorado.gis","r");

        //~ Simplify by removing for and initializing i
        for(i=0 ; i<1 ; i++) {
                fscanf(infile, "%s", shapefile_read);
                printf("%s", shapefile_read);
        }

        fclose(infile);
        return 0;
}

The string of the file is something like this:
M 2196.693 3021.689 L 2196.709 .....


"something like" isn't good enough. We need to know your file format.

I get these errors:
goo_0.01.c:67: warning: format ‘%s’ expects type ‘char *’, but
argument 3 has type ‘int’
goo_0.01.c:68: warning: format ‘%s’ expects type ‘char *’, but
argument 2 has type ‘int’

Do I want this to be a pointer or what?

who knows.

I made a guess as to what your data looked like and wrote this


#include <stdlib.h> /* exit() */
#include <stdio.h>


/*
The format of the file is something like this:
M 2196.693 3021.689 L 2196.709 .....
*/

void process_file (void)
{
FILE *in;
char ch [2];
double x, y;
int fields_read;

if ((in = fopen ("county.txt", "r")) == NULL)
{
fprintf (stderr, "can't open input file\n");
exit (EXIT_FAILURE);
}

while ((fields_read = fscanf (in, "%1s %lf %lf ", &ch, &x, &y)) ==
3)
{
printf ("read %s %f %f\n", ch, x, y);
}

printf ("read %d fields\n", fields_read);
printf ("no more valid records\n");

fclose (in);
}


int main (void)
{
process_file ();
return 0;
}



my data looked like this

M 2196.693 3021.689 L 2196.709 3022.678 K 2195.999 3033.356

My program assumes a pretty rigid syntax and fscanf() isn't
very flexible. Normally I'd suggest fgets() followed by sscanf()
but this may not be a good idea with lines that are 800k.

Perhaps fscanf(infile, "%10s", &token) to read chunks from the file
then check for a number using %lf. Retrying is much easier with scanf
().


--
Nick Keighley

10.3. Transput declarations
{ "So it does!" said Pooh, "It goes in!"
"So it does!" said Piglet, "And it comes out!"
"Doesn't it?" said Eeyore, "It goes in and out like
anything,"
Winnie-the-Pooh, A.A. Milne.}
"Revised Report on the Algorithmic Language ALGOL 68"
 
B

Ben Bacarisse

my data looked like this

M 2196.693 3021.689 L 2196.709 3022.678 K 2195.999 3033.356

My program assumes a pretty rigid syntax and fscanf() isn't
very flexible. Normally I'd suggest fgets() followed by sscanf()
but this may not be a good idea with lines that are 800k.

I get the feeling this is ideal territory for fscanf. You can read
the "code letter" with either fgetc(fp) or fscanf(fp, "%c", &code) and
then loop over numbers:

while (fscanf(fp, "%lf", &num) == 1)
if (room_to_store_it)
numbers[i++] = num;

Of course, I second your "we keen to know the format" comment. If it
is very complex, a more subtle parser may be needed.
Perhaps fscanf(infile, "%10s", &token) to read chunks from the file
then check for a number using %lf. Retrying is much easier with scanf
().

I think that will not simplify this particular case, though I have
to eat my words when the OP posts the actual data format.
 
D

deadpickle

the main problem is we've no little idea about what you are trying to
do.

Good point. What this is is a Gtk application that uses goocanvas to
draw shapefiles. This file contains the SVG string used by the
goocanvas call "path" to draw just that. I will probably try to use
shapelib to do this but right now I am having problems even reading in
a file. I am coming from perl and am not use to having to declare
formats so thanks for all the help.

This string that the file contains will be dropped into the Path call
for goocanvas. So in that matter it is important to have it contained
into one string but I'm not all that familiar with C so maybe there
are other ways.
Made these changes:

your layout is bizzare
#include <stdio.h>
int main( int   argc,char *argv[] )
{
        //~ Type Declaration

this is a pretty useless comment


        FILE *infile;
        int i;
        char shapefile_read[1];
        //~ Read shapefile file
        infile = fopen("county_colorado.gis","r");
        //~ Simplify by removing for and initializing i
        for(i=0 ; i<1 ; i++) {
                fscanf(infile, "%s", shapefile_read);
                printf("%s", shapefile_read);
        }

        fclose(infile);
        return 0;
}
The string of the file is something like this:
M 2196.693 3021.689 L 2196.709 .....

"something like" isn't good enough. We need to know your file format.
I get these errors:
goo_0.01.c:67: warning: format ‘%s’ expects type ‘char *’, but
argument 3 has type ‘int’
goo_0.01.c:68: warning: format ‘%s’ expects type ‘char *’, but
argument 2 has type ‘int’
Do I want this to be a pointer or what?

who knows.

I made a guess as to what your data looked like and wrote this

#include <stdlib.h>     /* exit() */
#include <stdio.h>

/*
The format of the file is something like this:
M 2196.693 3021.689 L 2196.709 .....
*/

void process_file (void)
{
    FILE *in;
    char ch [2];
    double x, y;
    int fields_read;

    if ((in = fopen ("county.txt", "r")) == NULL)
    {
        fprintf (stderr, "can't open input file\n");
        exit (EXIT_FAILURE);
    }

    while ((fields_read = fscanf (in, "%1s %lf %lf ", &ch, &x, &y)) ==
3)
    {
        printf ("read %s %f %f\n", ch, x, y);
    }

    printf ("read %d fields\n", fields_read);
    printf ("no more valid records\n");

    fclose (in);

}

int main (void)
{
    process_file ();
    return 0;

}

my data looked like this

M 2196.693 3021.689 L 2196.709 3022.678 K 2195.999 3033.356

My program assumes a pretty rigid syntax and fscanf() isn't
very flexible. Normally I'd suggest fgets() followed by sscanf()
but this may not be a good idea with lines that are 800k.

Perhaps fscanf(infile, "%10s", &token) to read chunks from the file
then check for a number using %lf. Retrying is much easier with scanf
().

--
Nick Keighley

10.3. Transput declarations
        { "So it does!" said Pooh, "It goes in!"
          "So it does!" said Piglet, "And it comes out!"
          "Doesn't it?" said Eeyore, "It goes in and out like
anything,"
          Winnie-the-Pooh, A.A. Milne.}
"Revised Report on the Algorithmic Language ALGOL 68"
 
B

Ben Bacarisse

deadpickle said:
[snipped]

the main problem is we've no little idea about what you are trying to
do.

Good point.
"something like" isn't good enough. We need to know your file
format.

You missed this bit. If you can describe the format, people here can
help you with the best way to read it.
 
B

BartC

Ben said:
deadpickle said:
[snipped]

the main problem is we've no little idea about what you are trying
to do.

Good point.
The string of the file is something like this:
M 2196.693 3021.689 L 2196.709 .....

"something like" isn't good enough. We need to know your file
format.

You missed this bit. If you can describe the format, people here can
help you with the best way to read it.

I think he said the entire file contents will be passed as a single string:
"This string that the file contains will be dropped into the Path call
for goocanvas. So in that matter it is important to have it contained
into one string but I'm not all that familiar with C so maybe there
are other ways."

In that case the format is very simple..
 
B

Ben Bacarisse

BartC said:
Ben said:
deadpickle said:
On Apr 20, 3:01 am, (e-mail address removed) wrote:
[snipped]

the main problem is we've no little idea about what you are trying
to do.

Good point.

The string of the file is something like this:
M 2196.693 3021.689 L 2196.709 .....

"something like" isn't good enough. We need to know your file
format.

You missed this bit. If you can describe the format, people here can
help you with the best way to read it.

I think he said the entire file contents will be passed as a single string:
"This string that the file contains will be dropped into the Path call
for goocanvas. So in that matter it is important to have it contained
into one string but I'm not all that familiar with C so maybe there
are other ways."

In that case the format is very simple..

Oh right. Missed that.

To the OP: since you are using GTK, you most likely have GNU getline.
This will do what you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top