Need help to save strings from a file to a structure

D

Dadio

Hi!
I have to take some strings from a file and put them in a record...
The various strings in the file are written on this way:
string1|string2|string3|string4|string5|

This is the program that i have just made...what's wrong?

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct table
{
char *protocol;
char *object;
char *destinat;
char *fax;
char *note;
};
FILE *fd; //file dove risiedono i dati
void main()
{
clrscr();
int x=0;
table *grid;
fd=fopen("c:\\registro.txt","r"); //file with 5 strings to
put in the record
for (x=0;x<5;x++) //for example 5

fscanf(fd,"%s|%s|%s|%s|%s|",grid[x].protocol,grid[x].object,grid[x].destinat,grid[x].fax,grid[x].note);

//this was just to verify that the strings were stored in the record
for (x=0;x<5;x++)
{
printf("%s %s %s %s
%s",grid[x].protocol,grid[x].object,grid[x].destinat,grid[x].fax,grid[x].note);
getch();
}
fclose(fd);
}


I've tried another method too...
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct table
{
char *protocol;
char *object;
char *destinat;
char *fax;
char *note;
};
FILE *fd;
void main()
{
clrscr();
int x=0;
table *grid;
char a=' ';
int b=0;
fd=fopen("c:\\registro.txt","r");
for (x=0;x<5;x++) //for example 5
{
while(a!='|')
{
a=fgetc(fd);
grid[x].protocol=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].object=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].destinat=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].fax=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].note=a;
b++;
}
} */
fclose(fd);
}


I'd like in particoular a solution for the 1st method...
I'm sorry if i'm writing stupid things but i'm still studying C at the
high school...
Thank u

PS:sorry for my english too
 
C

Chris Dollin

Dadio said:
Hi!
I have to take some strings from a file and put them in a record...
The various strings in the file are written on this way:
string1|string2|string3|string4|string5|

This is the program that i have just made...what's wrong?

#include<stdio.h>
#include<conio.h>

No such standard C header.
#include<string.h>
struct table
{
char *protocol;
char *object;
char *destinat;
char *fax;
char *note;
};
FILE *fd; //file dove risiedono i dati

There's no reason for this to be non-local. Put it inside `main`.
void main()

`main` should return `int`. `void main` is not portable.
{
clrscr();

Not a standard C function. Also unnecessary.
int x=0;
table *grid;

Syntax error. You mean `struct table`. Are you compiling this with a
C++ compiler by mistake? Assuming that it should be `struct table` ...

`grid` can contain a pointer-to-table value, but at the moment
is contains random rubbish.
fd=fopen("c:\\registro.txt","r"); //file with 5 strings to
put in the record

You should check for `fd` being non-null.
for (x=0;x<5;x++) //for example 5

fscanf(fd,"%s|%s|%s|%s|%s|",grid[x].protocol,grid[x].object,grid[x].destinat,grid[x].fax,grid[x].note);

`grid` doesn't point anywhere, so `grid[x]` is wrong. If it were right,
`grid[x].protocol` is a character pointer that doesn't point anywhere;
it's rubbish, so /that/'s wrong. If it /did/ point somewhere, the input
stream could contain a value that was longer than wherever it pointed,
so /that, too/ is wrong. Finally, if the input field has a space in it,
fscanf will stop reading there and then, so even that is wrong.

Your best bet, I would say, is to read the entire line into a local
string variable, using fgets and checking to make sure the string
fitted, and then look for the `|` characters. And make sure your
`grid` and the fields it points to are initialised.
 
M

Morris Dovey

Dadio (in (e-mail address removed)) said:

| Hi!
| I have to take some strings from a file and put them in a record...
| The various strings in the file are written on this way:
| string1|string2|string3|string4|string5|
|
| This is the program that i have just made...what's wrong?
|
| #include<stdio.h>
| #include<conio.h>
| #include<string.h>
| struct table
| {
| char *protocol;
| char *object;
| char *destinat;
| char *fax;
| char *note;
| };
| FILE *fd; //file dove risiedono i dati
| void main()
| {
| clrscr();
| int x=0;
| table *grid;
| fd=fopen("c:\\registro.txt","r"); //file with 5 strings
| to put in the record
| for (x=0;x<5;x++) //for example 5
|
|
fscanf(fd,"%s|%s|%s|%s|%s|",grid[x].protocol,grid[x].object,grid[x].de
stinat,grid[x].fax,grid[x].note);
|
| //this was just to verify that the strings were stored in the record
| for (x=0;x<5;x++)
| {
| printf("%s %s %s %s
|
%s",grid[x].protocol,grid[x].object,grid[x].destinat,grid[x].fax,grid[
x].note);
| getch();
| }
| fclose(fd);
| }

Just declaring a pointer does not initialize that pointer. You declare
grid to be a pointer to a struct table; but there is no actual table
structure anywhere!

The table structure, if it actually did exist, would contain five
pointers to char (protocol, object, etc) but those pointers would be
uninitialized and wouldn't point to any real memory.

With that for context, you ask fscanf() to read data from the file and
put it (oops!) where? Into five places that don't exist? This is a
recipe for disaster.

You need to allocate a real structure, and set up its pointers to
point to real memory.

[A side note: fd is properly declared and properly used; but "fp"
might be a better variable name - fd comes from 'file descriptor',
while fp comes from 'file pointer'. The stdio functions all use file
pointers.]
 
B

bert

Dadio said:
Hi!
I have to take some strings from a file and put them in a record...
The various strings in the file are written on this way:
string1|string2|string3|string4|string5|

This is the program that i have just made...what's wrong?

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct table
{
char *protocol;
char *object;
char *destinat;
char *fax;
char *note;
};
FILE *fd; //file dove risiedono i dati
void main()
{
clrscr();
int x=0;
table *grid;
fd=fopen("c:\\registro.txt","r"); //file with 5 strings to
put in the record
for (x=0;x<5;x++) //for example 5

fscanf(fd,"%s|%s|%s|%s|%s|",grid[x].protocol,grid[x].object,grid[x].destinat,grid[x].fax,grid[x].note);

//this was just to verify that the strings were stored in the record
for (x=0;x<5;x++)
{
printf("%s %s %s %s
%s",grid[x].protocol,grid[x].object,grid[x].destinat,grid[x].fax,grid[x].note);
getch();
}
fclose(fd);
}


I've tried another method too...
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct table
{
char *protocol;
char *object;
char *destinat;
char *fax;
char *note;
};
FILE *fd;
void main()
{
clrscr();
int x=0;
table *grid;
char a=' ';
int b=0;
fd=fopen("c:\\registro.txt","r");
for (x=0;x<5;x++) //for example 5
{
while(a!='|')
{
a=fgetc(fd);
grid[x].protocol=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].object=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].destinat=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].fax=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].note=a;
b++;
}
} */
fclose(fd);
}


I'd like in particoular a solution for the 1st method...
I'm sorry if i'm writing stupid things but i'm still studying C at the
high school...
Thank u

PS:sorry for my english too


You have a long way to go. Your declaration
table *grid;
declares a pointer to a table, but does not initialise it.
Something like
grid = malloc(sizeof(table));
would initialize the pointer to something more useful,
but not yet useful enough for you to operate on the
fields grid->protocol, grid->object etcetera.
Simpler for you is the declaration
table grid[5];
which allocates the space and lets you operate on
the fields grid[x].protocol, grid[x].object etcetera.
Notice the different punctuation according to whether
grid is of type table, or of type pointer-to-table.

You still have a long way to go. grid[0].protocol is
of type pointer-to-string, but does not point to anything,
so something like
grid[0].protocol = malloc(64);
is needed before you can use grid[0].protocol in an
fscanf statement. With five tables of five fields each,
you have a lot of malloc( ) to do (and to think about
the sizes for) before your program can work.
--
 
D

Dadio

bert ha scritto:
Dadio said:
Hi!
I have to take some strings from a file and put them in a record...
The various strings in the file are written on this way:
string1|string2|string3|string4|string5|

This is the program that i have just made...what's wrong?

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct table
{
char *protocol;
char *object;
char *destinat;
char *fax;
char *note;
};
FILE *fd; //file dove risiedono i dati
void main()
{
clrscr();
int x=0;
table *grid;
fd=fopen("c:\\registro.txt","r"); //file with 5 strings to
put in the record
for (x=0;x<5;x++) //for example 5

fscanf(fd,"%s|%s|%s|%s|%s|",grid[x].protocol,grid[x].object,grid[x].destinat,grid[x].fax,grid[x].note);

//this was just to verify that the strings were stored in the record
for (x=0;x<5;x++)
{
printf("%s %s %s %s
%s",grid[x].protocol,grid[x].object,grid[x].destinat,grid[x].fax,grid[x].note);
getch();
}
fclose(fd);
}


I've tried another method too...
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct table
{
char *protocol;
char *object;
char *destinat;
char *fax;
char *note;
};
FILE *fd;
void main()
{
clrscr();
int x=0;
table *grid;
char a=' ';
int b=0;
fd=fopen("c:\\registro.txt","r");
for (x=0;x<5;x++) //for example 5
{
while(a!='|')
{
a=fgetc(fd);
grid[x].protocol=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].object=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].destinat=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].fax=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].note=a;
b++;
}
} */
fclose(fd);
}


I'd like in particoular a solution for the 1st method...
I'm sorry if i'm writing stupid things but i'm still studying C at the
high school...
Thank u

PS:sorry for my english too


You have a long way to go. Your declaration
table *grid;
declares a pointer to a table, but does not initialise it.
Something like
grid = malloc(sizeof(table));
would initialize the pointer to something more useful,
but not yet useful enough for you to operate on the
fields grid->protocol, grid->object etcetera.
Simpler for you is the declaration
table grid[5];
which allocates the space and lets you operate on
the fields grid[x].protocol, grid[x].object etcetera.
Notice the different punctuation according to whether
grid is of type table, or of type pointer-to-table.

You still have a long way to go. grid[0].protocol is
of type pointer-to-string, but does not point to anything,
so something like
grid[0].protocol = malloc(64);
is needed before you can use grid[0].protocol in an
fscanf statement. With five tables of five fields each,
you have a lot of malloc( ) to do (and to think about
the sizes for) before your program can work.
--



tnk all very much ^_^
I'll try to correct it following your suggestions (mallocs or gets and
then divide-->i haven't tthought about this strategy...I have still
much to learn)
tnk u again:)
 
D

Dadio

Dadio ha scritto:
bert ha scritto:
Dadio said:
Hi!
I have to take some strings from a file and put them in a record...
The various strings in the file are written on this way:
string1|string2|string3|string4|string5|

This is the program that i have just made...what's wrong?

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct table
{
char *protocol;
char *object;
char *destinat;
char *fax;
char *note;
};
FILE *fd; //file dove risiedono i dati
void main()
{
clrscr();
int x=0;
table *grid;
fd=fopen("c:\\registro.txt","r"); //file with 5 strings to
put in the record
for (x=0;x<5;x++) //for example 5

fscanf(fd,"%s|%s|%s|%s|%s|",grid[x].protocol,grid[x].object,grid[x].destinat,grid[x].fax,grid[x].note);

//this was just to verify that the strings were stored in the record
for (x=0;x<5;x++)
{
printf("%s %s %s %s
%s",grid[x].protocol,grid[x].object,grid[x].destinat,grid[x].fax,grid[x].note);
getch();
}
fclose(fd);
}


I've tried another method too...
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct table
{
char *protocol;
char *object;
char *destinat;
char *fax;
char *note;
};
FILE *fd;
void main()
{
clrscr();
int x=0;
table *grid;
char a=' ';
int b=0;
fd=fopen("c:\\registro.txt","r");
for (x=0;x<5;x++) //for example 5
{
while(a!='|')
{
a=fgetc(fd);
grid[x].protocol=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].object=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].destinat=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].fax=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].note=a;
b++;
}
} */
fclose(fd);
}


I'd like in particoular a solution for the 1st method...
I'm sorry if i'm writing stupid things but i'm still studying C at the
high school...
Thank u

PS:sorry for my english too


You have a long way to go. Your declaration
table *grid;
declares a pointer to a table, but does not initialise it.
Something like
grid = malloc(sizeof(table));
would initialize the pointer to something more useful,
but not yet useful enough for you to operate on the
fields grid->protocol, grid->object etcetera.
Simpler for you is the declaration
table grid[5];
which allocates the space and lets you operate on
the fields grid[x].protocol, grid[x].object etcetera.
Notice the different punctuation according to whether
grid is of type table, or of type pointer-to-table.

You still have a long way to go. grid[0].protocol is
of type pointer-to-string, but does not point to anything,
so something like
grid[0].protocol = malloc(64);
is needed before you can use grid[0].protocol in an
fscanf statement. With five tables of five fields each,
you have a lot of malloc( ) to do (and to think about
the sizes for) before your program can work.
--



tnk all very much ^_^
I'll try to correct it following your suggestions (mallocs or gets and
then divide-->i haven't tthought about this strategy...I have still
much to learn)
tnk u again:)




Ahem...I've met some probs cutting the string into more substrings when
i meet the character "|" as Chris suggested...
Is there any function or any specific algoritm to do that?
Pls show me
 
C

Clever Monkey

Dadio said:
Dadio ha scritto:
bert ha scritto:
[...]
Ahem...I've met some probs cutting the string into more substrings when
i meet the character "|" as Chris suggested...
Is there any function or any specific algoritm to do that?
Pls show me
If you want to split that string into multiple strings based on the
delimiter, you probably want to look into strtok(). You can also walk
the string with strstr() (and friends in string.h) if you need to do
something else with the string.
 
M

Morris Dovey

| Dadio wrote:

|| Ahem...I've met some probs cutting the string into more substrings
|| when i meet the character "|" as Chris suggested...
|| Is there any function or any specific algoritm to do that?
|| Pls show me

I have some code that'll do the job; but I think that you'll learn
more by writing and debugging your own code. If you'd like to see
mine, download:

www.iedu.com/mrd/c/tokfile.c tokenizes a file using
www.iedu.com/mrd/c/tokenize.c tokenizes a line
www.iedu.com/mrd/c/getsm.c reads a line

There's a tiny function in tokenize.c that you'd need to modify to
split on '|' - but that should be an easy change.
 
D

Dadio

Morris Dovey ha scritto:
| Dadio wrote:

|| Ahem...I've met some probs cutting the string into more substrings
|| when i meet the character "|" as Chris suggested...
|| Is there any function or any specific algoritm to do that?
|| Pls show me

I have some code that'll do the job; but I think that you'll learn
more by writing and debugging your own code. If you'd like to see
mine, download:

www.iedu.com/mrd/c/tokfile.c tokenizes a file using
www.iedu.com/mrd/c/tokenize.c tokenizes a line
www.iedu.com/mrd/c/getsm.c reads a line

There's a tiny function in tokenize.c that you'd need to modify to
split on '|' - but that should be an easy change.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto



Oh my god...I've another probs:
I've used strtok( )...
When I put the string in each record field it's all ok (I can see it
from printf( ) )
but if I try to print at the end of the program, it prints for 5 times
only THE LAST record!!
I've done the debugging and I saw that all the content of the structure
changes after the fgets( ) instruction...
why?how can i solve this problem?
PS:I've changed ' | ' with ' blank '
PPS:It's only a part arranged of a bigger program...unfortunately the
key-part -.-'' if i do this part the program is done...

This is my program now:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct table
{
char *protocol;
char *objecyt
char *destinat;
char *fax;
char *note;
};
void main()
{
FILE *fp;
clrscr();
int x=0;
char *input;
table *grid;
grid(table*)malloc(sizeof(table)); //is it useful??
fd=fopen("c:\\registro.txt","r");
for (x=0;x<5;x++) //for example 5
{
fgets(input,500,fp); //This seems to be
the EVIL instruction
grid[x].protocol = strtok(input," ");
printf("%s\n", grid[x].protocol); //printing is correct
grid[x].object = strtok(NULL, " ");
printf("%s\n", grid[x].object); // "
grid[x].destinat=strtok(NULL," ");
printf("%s\n",grid[x].destinat); // "
grid[x].fax=strtok(NULL," ");
printf("%s\n",grid[x].fax); // "
grid[x].note=strtok(NULL," ");
printf("%s\n",grid[x].note); // "
}
getch();
for (x=0;x<5;x++)

printf("%s\n%s\n%s\n%s\n%s\n",grid[x].protocol,grid[x].object,grid[x].destinat,grid[x].fax,grid[x].note);
//printing uncorrect
getch();
fclose(fp);
}

If my file is for example:
1 11 111 1111 11111
2 22 222 2222 22222
3 33 333 3333 33333
4 44 444 4444 44444
5 55 555 5555 55555

After running the program it prints on the screen

1
11
111
1111
11111
2
22
222
2222
22222
3
33
333
3333
33333
4
44
444
4444
44444
5
55
555
5555
55555
This is the first loop and it's correct
5
55555
111
1111
11111
5
55
555
5555
55555
5
55
555
5555
55555
5
55
555
5555
55555
5
55
555
5555
55555
This is the second loop
The "evil" instruction make the program to print this...
Why the content of the record is changed with a fgets () instruction?
What's wrong?
How can i correct it?
Thank u for your patient (I'm sorry for my unknowledge)
 
A

Andrew Poelstra

Oh my god...I've another probs:

Oh my god...clean posts will get better replies.
I've used strtok( )...
When I put the string in each record field it's all ok (I can see it
from printf( ) )
but if I try to print at the end of the program, it prints for 5 times
only THE LAST record!!

You know that strtok modifies the string that you pass to it, right? And
so when you use it, it destroys what was before the delimiter.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top