473,378 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Need help to save strings from a file to a structure

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[b]=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].object[b]=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].destinat[b]=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].fax[b]=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].note[b]=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

Jun 29 '06 #1
9 2024
Dadio wrote:
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.

--
Chris "0xdeadbeef" Dollin
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

Jun 29 '06 #2
Dadio (in 11********************@d56g2000cwd.googlegroups.co m) 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.]

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Jun 29 '06 #3

Dadio wrote:
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[b]=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].object[b]=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].destinat[b]=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].fax[b]=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].note[b]=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.
--

Jun 29 '06 #4

bert ha scritto:
Dadio wrote:
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[b]=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].object[b]=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].destinat[b]=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].fax[b]=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].note[b]=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:)

Jun 29 '06 #5

Dadio ha scritto:
bert ha scritto:
Dadio wrote:
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[b]=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].object[b]=a;
b++;
}
b=0;a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].destinat[b]=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].fax[b]=a;
b++;
}
b=0; a=' ';
while(a!='|')
{
a=fgetc(fd);
grid[x].note[b]=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

Jun 29 '06 #6
Dadio wrote:
Dadio ha scritto:
bert ha scritto:
Dadio wrote:
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|
[...] 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.
Jun 29 '06 #7
| 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
Jun 30 '06 #8

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)

Jun 30 '06 #9
On 2006-06-30, Dadio <da******@hotmail.it> wrote:
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.

--
Andrew Poelstra <http://www.wpsoftware.net/blog>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Jun 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: MrNobody | last post by:
I would like to make a quick little hex editor so I need to display hex string pairs like a grid- if anyone is familiar with hex editors you'll know what I mean. I could accomplish this by using a...
0
by: Miguel Dias Moura | last post by:
Hello, I am working on an Asp.Net 2.0 / SQL 2005 web site. I am using profile to save the users info on the database. For example, I have the following structure: Public Structure Name...
16
by: C++ Hell | last post by:
Hey everyone just designing a quiz for school and managed to write the code for the questions and answers thou i have to add scores and at the end an overall score does anyone have any idea what to...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
18
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before....
3
by: Pebble | last post by:
Hi folks, I have a WebService that simply acts as a thin wrapper to a .Net dll. Literally almost zero code in the WebService. Parameters passed to the WebService are simply used as...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
1
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string arrays. Bascially the vb6 programmer stored all...
2
by: Anders B | last post by:
I want to make a program that reads the content of a LUA array save file.. More precicely a save file from a World of Warcraft plugin called CharacterProfiler, which dumps alot of information about...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.