Need help with problem

S

Scott

Hi,

a problem with this following code is really bugging me.

tform = fopen(country, "r");

fseek(tform, 9L, SEEK_SET);
fgets(player2, 38, tform);
printf("Player Name (save): %s", player);
printf("Player Name (form): %s", player2);
fseek(tform, 10L, SEEK_CUR);
fgets(country2, 38, tform);
printf("Country Name (save); %s", country);
printf("Country Name (form): %s", country2);
fseek(tform, 18L, SEEK_CUR);
fscanf(tform, "%i \n", &app_budget);
fseek(tform, 16L, SEEK_CUR);
fscanf(tform, "%i \n", &hea_budget);
fseek(tform, 19L, SEEK_CUR);
fscanf(tform, "%i \n", &edu_budget);
fseek(tform, 18L, SEEK_CUR);
fscanf(tform, "%i \n", &sec_budget);
fseek(tform, 16L, SEEK_CUR);
fscanf(tform, "%i \n", &soc_budget);
fseek(tform, 17L, SEEK_CUR);
fscanf(tform, "%i \n", &eco_budget);
fseek(tform, 11L, SEEK_CUR);
fscanf(tform, "%i", &tax_change);

When I run the code as written above it will compile without errors,
but when I run the program I get a "Segmentation fault (core dumped)"
error. But, if I switch the country variable in the Fopen to
"Canada" (the value of the variable) it opens the file called Canada
fine and works perfectly. I know the variable is Canada as I had a
printf function write the value of the variable and it says "Canada".

To make things even more weird if I comment out all the above code
except the fopen with the variable it will not show the error. So, it
seems it can read the file, but can not do anything with it.

What I am trying to do is have a while loop that completes turns for a
PBEM. I have the while loop working great going though the list of
players one by one and stopping at the end of the file, but I need
this code above to load the turn sheet for each of the players. I
could use several "if"s to load the files, but that would mean I would
need to re-write the code and re-compile whenever I have a change in
players.

Any advice on how to fix this would be greatly appretiated. I am very
new to C and a all-round programming novice, so, please dum down any
help :)

--
Your friend,
Scott

Sent to you from a 100% Linux computer using Kubuntu Version 7.04
(Feisty Fawn)
 
C

Chris Dollin

Scott said:
Hi,

a problem with this following code is really bugging me.

tform = fopen(country, "r");

Check that the file open succeeded.
fseek(tform, 9L, SEEK_SET);
fgets(player2, 38, tform);
printf("Player Name (save): %s", player);
printf("Player Name (form): %s", player2);
fseek(tform, 10L, SEEK_CUR);
fgets(country2, 38, tform);
printf("Country Name (save); %s", country);
printf("Country Name (form): %s", country2);
fseek(tform, 18L, SEEK_CUR);
fscanf(tform, "%i \n", &app_budget);

Check that exactly one item is converted.
fseek(tform, 16L, SEEK_CUR);
fscanf(tform, "%i \n", &hea_budget);
fseek(tform, 19L, SEEK_CUR);
fscanf(tform, "%i \n", &edu_budget);
fseek(tform, 18L, SEEK_CUR);
fscanf(tform, "%i \n", &sec_budget);
fseek(tform, 16L, SEEK_CUR);
fscanf(tform, "%i \n", &soc_budget);
fseek(tform, 17L, SEEK_CUR);
fscanf(tform, "%i \n", &eco_budget);
fseek(tform, 11L, SEEK_CUR);
fscanf(tform, "%i", &tax_change);

Oh my goodness.

Why, oh why, all these magic numbers and fseeks? What's the
layout of the file supposed to be, and why is it so dependent
on fixed formats?

It looks like ... a player name, a country name, six budgets,
and a tax change. Why not expect nine lines like this:

player: their name
country: its name
app-budget: number
(etc)

`fgets` each line in turn, check that the tag is what you expect,
and the extract the information from the rest of that line. No
`fseeks` and the file is now self-documenting. (And easier to
test -- because now the test data doesn't include a FILE* and
a position, just a string for the line and the expected value of
the item.)
When I run the code as written above it will compile without errors,
but when I run the program I get a "Segmentation fault (core dumped)"
error. But, if I switch the country variable in the Fopen to
"Canada" (the value of the variable) it opens the file called Canada
fine and works perfectly. I know the variable is Canada as I had a
printf function write the value of the variable and it says "Canada".

Always aim to show real, complete (but trimmed to minimal) code.
If you don't understand the problem, you don't know what's safe
to leave out.

--
JUC 2007, submit: http://hpl.hp.com/conferences/juc2007/submission.html
"Our future looks secure, but it's all out of our hands." /Man and Machine/
- Magenta

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
 
G

gw7rib

Hi,

a problem with this following code is really bugging me.

tform = fopen(country, "r");

fseek(tform, 9L, SEEK_SET);
fgets(player2, 38, tform);

When I run the code as written above it will compile without errors,
but when I run the program I get a "Segmentation fault (core dumped)"
error. But, if I switch the country variable in the Fopen to
"Canada" (the value of the variable) it opens the file called Canada
fine and works perfectly. I know the variable is Canada as I had a
printf function write the value of the variable and it says "Canada".

To make things even more weird if I comment out all the above code
except the fopen with the variable it will not show the error. So, it
seems it can read the file, but can not do anything with it.

Guessing wildly, I would guess that there is some subtle difference
between the actual value of the variable and what you think it is. A
control character, perhaps. As Chris has said, you ought to check
whether the fopen succeeded. If it opens with a literal and doesn't
open with a variable then the variable probably has the wrong value.
Try doing a strcmp to check.

One possible problem (I'm guessing even more wildly here) may be that
you are using backslashes wrongly in your code. For example,

fopen("\nations\turkey", "r");

will not work - the name has an enter and a tab in it. You would need:

fopen("\\nations\\turkey", "r");

or

fopen("/nations/turkey", "r");

Hope this is useful.
Paul.
 
S

Scott

Why, oh why, all these magic numbers and fseeks? What's the
layout of the file supposed to be, and why is it so dependent
on fixed formats?
I am trying to have the program read text from a html form; something
I allready dud sucessfully earlier in the program. The format of the
text form is below.

Player = Scott Lewin
Country = Canada
Approval Budget = 1000
Health Budget = 1000
Education Budget = 1000
Security Budget = 1000
Social Budget = 1000
Economy Budget = 1000
Tax Rate = 1

It looks like ... a player name, a country name, six budgets,
and a tax change.
That is exactly correct.

`fgets` each line in turn, check that the tag is what you expect,
and the extract the information from the rest of that line. No
`fseeks` and the file is now self-documenting.
I apologize for being such a newbie, but how do I do this?


Always aim to show real, complete (but trimmed to minimal) code.
If you don't understand the problem, you don't know what's safe
to leave out.
Okay, that makes sense. I have added the complete section of code
below without the debugging I added in.

void turn(void)
{
FILE *mfile;
FILE *clist;
FILE *cfile;
FILE *tform;
char country[40];
char country2[40];
char player[40];
char player2[40];
char player_lng[50];
int i;
int type;
int phil;
int approval;
int health;
int education;
int security;
int social;
int economy;
int population;
int size;
int tax;
int funds;
int app_budget;
int hea_budget;
int edu_budget;
int sec_budget;
int soc_budget;
int eco_budget;
int tax_change;

clist = fopen("./master_files/clist", "r");

while(!feof(clist))
{
fgets(player, 38, clist);

cfile = fopen(player, "r+");

fscanf(cfile, "%[^/]/ %[^/]/ %i/ %i/ %i/ %i/ %i/ %i/ %i/ %i/ %i/ %i/
%i/ %i", player_lng, country, &type, &phil, &approval, &health,
&education, &security, &social, &economy, &population, &size, &tax,
&funds);


printf("Country: %s", country);
tform = fopen(country, "r");

fseek(tform, 9L, SEEK_SET);
fgets(player2, 38, tform);
printf("Player Name (save): %s", player);
printf("Player Name (form): %s", player2);
fseek(tform, 10L, SEEK_CUR);
fgets(country2, 38, tform);
printf("Country Name (save); %s", country);
printf("Country Name (form): %s", country2);
fseek(tform, 18L, SEEK_CUR);
fscanf(tform, "%i \n", &app_budget);
fseek(tform, 16L, SEEK_CUR);
fscanf(tform, "%i \n", &hea_budget);
fseek(tform, 19L, SEEK_CUR);
fscanf(tform, "%i \n", &edu_budget);
fseek(tform, 18L, SEEK_CUR);
fscanf(tform, "%i \n", &sec_budget);
fseek(tform, 16L, SEEK_CUR);
fscanf(tform, "%i \n", &soc_budget);
fseek(tform, 17L, SEEK_CUR);
fscanf(tform, "%i \n", &eco_budget);
fseek(tform, 11L, SEEK_CUR);
fscanf(tform, "%i", &tax_change);


}

fclose(clist);
}
 
S

Scott

Guessing wildly, I would guess that there is some subtle difference
between the actual value of the variable and what you think it is. A
control character, perhaps. As Chris has said, you ought to check
whether the fopen succeeded.
I added in the following line of code and, unless I did it wrong,
there is an error in opening the file as I got the text "Cannot open
turn form".

if ((tform = fopen(country, "r")) == NULL)
{
printf("Cannot open turn form");
return;
}

Try doing a strcmp to check.


One possible problem (I'm guessing even more wildly here) may be that
you are using backslashes wrongly in your code. For example,
To make things simple I am trying just to read the file from the same
folder as the program. Also, I'm on a Linux system, so I allways use
the / slash.

Your friend,
Scott
 
S

Scott

Try doing a strcmp to check.
Thanks to the list I am slowly getting closer to the problem. I used
the strcmp command and found out that the variable is not what it
should be.

Your friend,
Scott
 
S

Scott

Try doing a strcmp to check.
Thanks to the list I am slowly getting closer to the problem. I used
the strcmp command and found out that the variable is not what it
should be.

Your friend,
Scott
 
C

CBFalconer

Scott said:
a problem with this following code is really bugging me.

tform = fopen(country, "r");
fseek(tform, 9L, SEEK_SET);
fgets(player2, 38, tform);
printf("Player Name (save): %s", player);
printf("Player Name (form): %s", player2);
fseek(tform, 10L, SEEK_CUR);
fgets(country2, 38, tform);
printf("Country Name (save); %s", country);
printf("Country Name (form): %s", country2);
fseek(tform, 18L, SEEK_CUR);
fscanf(tform, "%i \n", &app_budget);
fseek(tform, 16L, SEEK_CUR);
fscanf(tform, "%i \n", &hea_budget);
fseek(tform, 19L, SEEK_CUR);
fscanf(tform, "%i \n", &edu_budget);
fseek(tform, 18L, SEEK_CUR);
fscanf(tform, "%i \n", &sec_budget);
fseek(tform, 16L, SEEK_CUR);
fscanf(tform, "%i \n", &soc_budget);
fseek(tform, 17L, SEEK_CUR);
fscanf(tform, "%i \n", &eco_budget);
fseek(tform, 11L, SEEK_CUR);
fscanf(tform, "%i", &tax_change);

When I run the code as written above it will compile without errors,

Nonsense. When I compile the above code all I get is errors.
 
S

Scott

Try doing a strcmp to check.
Thanks to the list I am slowly getting closer to the problem. I used
the strcmp command and found out that the variable is not what it
should be.

Your friend,
Scott
 
S

Scott

I think I know what the problem is now, I think it was the kind of
file. Is there certain type of Text files C can not read from?

What I did was re-save the file using a different text editor (Kate
instead of Kedit) and it worked perfectly.

Your friend,
Scott
 
S

Scott

I think I know what the problem is now, I think it was the kind of
file. Is there certain type of Text files C can not read from?

What I did was re-save the file using a different text editor (Kate
instead of Kedit) and it worked perfectly.

Your friend,
Scott
 
I

Ian Collins

Scott said:
I think I know what the problem is now, I think it was the kind of
file. Is there certain type of Text files C can not read from?
Which problem? Please quote context and stop posting the same thing
multiple times.

If C couldn't read a file, what could?
 
C

CBFalconer

Scott said:
I think I know what the problem is now, I think it was the kind of
file. Is there certain type of Text files C can not read from?

What I did was re-save the file using a different text editor (Kate
instead of Kedit) and it worked perfectly.

Clear as mud. What problem?
 
S

Scott

Please quote context and stop posting the same thing
multiple times.
Google groups seems to be a little screwed. It reposted my last
message sent every time I pressed the re-load button on my browser.

If C couldn't read a file, what could?
I could open the file in Kedit (which is a simple text editor for KDE)
and read it fine.
 
O

osmium

Scott said:
I think I know what the problem is now, I think it was the kind of
file. Is there certain type of Text files C can not read from?

What I did was re-save the file using a different text editor (Kate
instead of Kedit) and it worked perfectly.

I haven't been following this thread, so this may be unrealted to your
problem.

But different editors can indeed save files differently WRT the '\n'
character. In ASCII, it can be stored as LF, FF or a combination of those
two In Windows, WordPad likes Unix files, Notepad does not. I am not sure
the *save* is different but there is no reason it couldn't be. This is the
crux of the binary/text files problem in C.
 
R

Richard Tobin

osmium said:
But different editors can indeed save files differently WRT the '\n'
character. In ASCII, it can be stored as LF, FF or a combination of those

Form feed? Surely you mean CR (carriage return). The combination CR-LF
of course corresponds to how old mechanical typewriters worked.

-- Richard
 
O

osmium

Richard Tobin said:
Form feed? Surely you mean CR (carriage return). The combination CR-LF
of course corresponds to how old mechanical typewriters worked.

Good God, yes!
 
S

Scott

I haven't been following this thread, so this may be unrealted to your
problem.

But different editors can indeed save files differently WRT the '\n'
character.
Yes, this does pertain to my problem. Is there a text format that is
good to save when you plan to have a C program read it? Even though I
have the program working now, I just want to make sure I don't run
into this problem again in the future.

Your friend,
Scott
 
W

Walter Roberson

Yes, this does pertain to my problem. Is there a text format that is
good to save when you plan to have a C program read it? Even though I
have the program working now, I just want to make sure I don't run
into this problem again in the future.

No, text formats are system dependant. There is no portable text-file
format, and the same system can end up supporting multiple text-file
formats. (I have a couple of Unix-like subsystems for Windows XP, and
the programs that run under those subsystems use a different text-file
format than XP does.)

The one thing you know about text streams is that if you write
one with a C implementation, then the file should read in the same
way with that same implementation -- provided you stuck to text
characters and \n . You don't know that a different version
of the same software will handle the reading the same way
(probably yes, though), and you don't know that the same tool suite
on a different system would produce exactly the same disk file for
the text (e.g., gcc on XP might produce a different file than gcc on
Solaris.)

Historically, this format difference was handled by ftp'ing the
file from one system to another using FTP's "ASCII" mode ("TYPE A").
ASCII mode told the sending system to convert the file format
to a standard representation while sending it, and told the
receiving system to do whatever it needed to do with the standard
format to make it usable as a text file by the receiving system.
(This process could also involve character set translations,
if for example one of the systems was using EBCDIC.)

If you want your program to be able to handle all the different
text file formats on your system, then you will have to start
by figuring out what all the text file formats -are-, exactly,
and then coding your program to open the file in binary format
and handle all the reads through a routine that would convert
the detected format into a standard format; then instead of
using fscanf(), use sscanf() and so on. (For symmetry you'd
want output routines that knew all the output formats.)
 

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

Similar Threads

Need Help with error 0
Why can't use fgetc() in SWITCH with CASE? 15
Help with code 0
Help with my responsive home page 2
Help With a Script 5
Python battle game help 2
Alter line of file 4
Problem with scanf 7

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top