f



Need help with problem

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)

0
sclewin (17)
3/29/2007 11:58:54 AM
comp.lang.c 30657 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

18 Replies
612 Views

Similar Articles

[PageSpeed] 47

Scott wrote:

> 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

0
chris.dollin (1683)
3/29/2007 12:48:05 PM
On 29 Mar, 12:58, "Scott" <scle...@gmail.com> wrote:
> Hi,
>
>     a problem with this following code is really bugging me.
>
> tform = fopen(country, "r");
>
>         fseek(tform, 9L, SEEK_SET);
>         fgets(player2, 38, tform);

<snip>

> 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.



0
gw7rib (471)
3/29/2007 8:34:50 PM
On Mar 29, 8:48 am, Chris Dollin <chris.dol...@hp.com> wrote:


> 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);
}


0
sclewin (17)
3/30/2007 12:06:56 AM
On Mar 29, 4:34 pm, gw7...@aol.com wrote:

> 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


0
sclewin (17)
3/30/2007 12:24:17 AM
On Mar 29, 4:34 pm, gw7...@aol.com wrote:

> 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

0
sclewin (17)
3/30/2007 1:05:36 AM
On Mar 29, 4:34 pm, gw7...@aol.com wrote:

> 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

0
sclewin (17)
3/30/2007 1:06:29 AM
Scott wrote:
> 
>     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.

-- 
Chuck F (cbfalconer at maineline dot net)
   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>



-- 
Posted via a free Usenet account from http://www.teranews.com

0
cbfalconer (19194)
3/30/2007 1:36:35 AM
On Mar 29, 4:34 pm, gw7...@aol.com wrote:

> 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

0
sclewin (17)
3/30/2007 2:11:53 AM
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


0
sclewin (17)
3/30/2007 2:49:50 AM
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


0
sclewin (17)
3/30/2007 2:50:07 AM
Scott wrote:
> 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?

-- 
Ian Collins.
0
ian-news (10155)
3/30/2007 2:57:13 AM
Scott wrote:
> 
> 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?

-- 
Chuck F (cbfalconer at maineline dot net)
   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>



-- 
Posted via a free Usenet account from http://www.teranews.com

0
cbfalconer (19194)
3/30/2007 4:44:50 AM
On Mar 29, 10:57 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> 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.


0
sclewin (17)
3/30/2007 12:31:38 PM
"Scott" writes:

>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. 


0
r124c4u1022 (2303)
3/30/2007 2:22:43 PM
In article <574kpjF2bq4gfU1@mid.individual.net>,
osmium <r124c4u102@comast.net> wrote:

>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

-- 
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
0
richard91 (3692)
3/30/2007 2:26:25 PM
"Richard Tobin" writes:

>>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.

Good God, yes!


0
r124c4u1022 (2303)
3/30/2007 2:43:04 PM
On Mar 30, 10:22 am, "osmium" <r124c4u...@comcast.net> wrote:
> 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


0
sclewin (17)
3/30/2007 4:11:17 PM
In article <1175271077.493518.58890@y66g2000hsf.googlegroups.com>,
Scott <sclewin@gmail.com> wrote:
>On Mar 30, 10:22 am, "osmium" <r124c4u...@comcast.net> wrote:

>> 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.

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.)
-- 
  Prototypes are supertypes of their clones.           -- maplesoft
0
roberson2 (8602)
3/30/2007 4:34:14 PM
Reply: