473,545 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "Segmentati on 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 2073
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 "Segmentati on 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 "Segmentati on 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("\\nation s\\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...@h p.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(cli st))
{
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

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

Similar topics

4
2729
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 hope that someone could help me out. As said in an earlier post I am new to c++, this is alot harder to do than VB. I am for the most part self...
9
2913
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 a device. The definition of the table is as follows: CREATE TABLE devicedata ( device_id int NOT NULL REFERENCES devices(id), -- id in the...
19
4072
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 the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can...
3
10616
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 that are hard to find. The main problem I am having right now is that I have a report that is sorted by one of these lookup fields and it only displays...
2
1471
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 where users can have profiles (as with most websites) but each users home page (profile page) will/should have the ability to be dynamic and...
16
2508
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 uses IE to talk with a server. The user on the client (IE) sees an ASP net page containing a TextBox. He can write some text in this text box and...
8
2723
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 the sequence nos and it should be such that i can access it through the structure .plz help me .
20
4233
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 is structured as an upside-down tree, and (if I remember correctly) never more than 4 levels. The site basically grew (like the creeping black...
13
5767
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 CatalogImage where imgID also needs to be placed. Below is my code behind to carry the catID using the Select @@Identity and insert imgID to the bridge...
25
3088
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 course then the file doesn't get seen by the user. Is there any command that essentially executes the code and then echo's it? something that...
0
7490
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7449
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6009
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.