473,395 Members | 1,919 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,395 software developers and data experts.

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)

Mar 29 '07 #1
18 2056
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

Mar 29 '07 #2
On 29 Mar, 12:58, "Scott" <scle...@gmail.comwrote:
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.

Mar 29 '07 #3
On Mar 29, 8:48 am, Chris Dollin <chris.dol...@hp.comwrote:

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);
}
Mar 30 '07 #4
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
Mar 30 '07 #5
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

Mar 30 '07 #6
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

Mar 30 '07 #7
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

Mar 30 '07 #8
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

Mar 30 '07 #9
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
Mar 30 '07 #10
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
Mar 30 '07 #11
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.
Mar 30 '07 #12
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

Mar 30 '07 #13
On Mar 29, 10:57 pm, Ian Collins <ian-n...@hotmail.comwrote:
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.
Mar 30 '07 #14
"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.
Mar 30 '07 #15
In article <57*************@mid.individual.net>,
osmium <r1********@comast.netwrote:
>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.
Mar 30 '07 #16
"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!
Mar 30 '07 #17
On Mar 30, 10:22 am, "osmium" <r124c4u...@comcast.netwrote:
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
Mar 30 '07 #18
In article <11*********************@y66g2000hsf.googlegroups. com>,
Scott <sc*****@gmail.comwrote:
>On Mar 30, 10:22 am, "osmium" <r124c4u...@comcast.netwrote:
>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
Mar 30 '07 #19

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

Similar topics

4
by: kazack | last post by:
I posted a similiar question in this newsgroup already and got an answer which I already knew but didn't get the answer I was looking for so I am reposting the code and question differently in the...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
2
by: bryan | last post by:
I have a situation that's pretty delicate that I need some help on. I've been stumped for awhile I just need some advice on the best possible solution. The problem: I have a site I'm making...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
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...
13
by: PinkBishop | last post by:
I am using VS 2005 with a formview control trying to insert a record to my access db. The data is submitted to the main table no problem, but I need to carry the catID to the bridge table...
25
by: Jon Slaughter | last post by:
I have some code that loads up some php/html files and does a few things to them and ultimately returns an html file with some php code in it. I then pass that file onto the user by using echo. Of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.