![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 8
| I am trying to write a program in C in which i will be changing the value of a particular field based on the input of the user. I understand that there is no way in C language where one can just simply change a line (or a portion of it) in a file. Rather the entire file has to be copied into another file and somewhere in the process of copying you can change the field.(Please comment on this if you think i am wrong) what i am trying to do is to read from a file(which has some records and each record has 4 fields). the data in the file is somehow like this(Numbers represent Gold,Silver and Bronze: USA: 47 72 33 Canada: 55 21 33 england: 55 40 71 ....... the format remains the same throughout the file. my program has to change any of those three numbers for a particular name(which will be provided by the user). users is asked to give 2 inputs: Name of the country, which is hold in "char country[5]", and 'a letter and a number' for identifying which field needs to be changed and by how much. following is the way i tried to code it but it wont work. Code: char countrya[50][200];
int golda[200],bronzea[200],silvera[200],i,count=0;
FILE *fpt1;
char country[50],countrynew[50],input;
// to count the number of lines in the file
while(4==fscanf(fpt1,"%[^:]: %d %d %d\n", countrynew, &gold, &bronze, &silver))
count++;
// grap all the data into arrays.The problem is somewhat here as nothing is transfered to the arrays
for (i=0;i<=count;i++)
{
fscanf(fpt1,"%[^:]: %d %d %d\n", countrya[i], golda[i], bronzea[i], silvera[i]);
}
// char country[50] holds the name provided by the user and then i tried to check and see which record holds that country so that i change the required field.
for (i=0;i<=count;i++)
{
if (0==strcmp(countrya[i],country))
{
if (input == 'g' || input == 'G')
golda[i]= newgold;
else if (input =='s' || input =='S')
silvera[i] = newsilver;
else if (input == 'b' || input == 'B')
bronzea[i] == newbronze;
else
;
}
}
// now i tried to remove the file and create a new one and put the data hold in the arrays in that one.
fclose(fpt1);
remove(fpt1);
fpt1=fopen(filename,"a");
for (i = 0 ; i <= count ; i++)
{
fprintf(fpt1,"%s: %d %d %d\n",countrya[i],golda[i],bronzea[i],silvera[i]);
}
fclose(fpt1);
: 0 0 0 : 0 0 0 ....... when i run it using the linux cc compiler. Please help, |
| atif7865 is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| There's no need to count the number of countries in the file, as you don't do anything with it. So you might as well use that original 4==fscanf loop to put things in the arrays proper, rather than into temporary variables that get overwritten. And files are more like sheets of paper than mobius strips; once you get to the bottom, you don't automatically go back up to the top. If you want to reread a file, you'll have to call rewind or fseek to get back to the beginning of the file. |
| tabstop is offline | |
| | #3 | |
| Registered User Join Date: Nov 2008
Posts: 8
| Quote:
| |
| atif7865 is offline | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| You got the ampersands right everywhere else.... |
| tabstop is offline | |
| | #5 | ||
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Do you want 50 countries with 200 max characters for the country's name or vice-versa? Quote:
Quote:
Increment countrya[i] before reading the next line else it'll overwrite the value stored there previously. | ||
| itCbitC is offline | |
| | #6 | |
| Registered User Join Date: Nov 2008
Posts: 8
| Quote:
well i need 200 countries with 50 characters max. yes, i think i can get rid of counting the number of the lines. as far as you last response, i just tried using ampersand too, it still isnt working. Am I using the two dimensional array of countrya in the fscanf rightly?? | |
| atif7865 is offline | |
| | #7 |
| Registered User Join Date: Nov 2008
Posts: 8
| hi, thanks for reply,, i have modified the code a bit but am getting an error. Code: char ch;
char country[50],countrynew[50],input;
int gold,bronze,silver,count=0,newgold,newbronze,newsilver,newcount,i;
char countrya[200][50];
int golda[200],bronzea[200],silvera[200];
FILE *fpt1;
while(4==fscanf(fpt1,"%[^:]: %d %d %d\n", &countrynew, &gold, &bronze, &silver))
{
countrya[i] = countrynew;
golda[i] = gold;
bronzea[i] = bronze;
silvera[i] = silver;
i++;
}
.........
|
| atif7865 is offline | |
| | #8 | |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| You don't need that many variables though that won't give you an error since your main problem still remains; shown in red below. Quote:
Code: fscanf(fpt1,"%[^:]: %d %d %d\n", &countrya[i][0], &golda[i], &bronzea[i], &silvera[i]) | |
| itCbitC is offline | |
| | #9 | |
| Registered User Join Date: Sep 2008
Posts: 5
| Quote:
Code: typedef struct{
char country_name[50];
int gold;
int silver;
int bronze;
} SPORTS;
SPORTS Sports1[200] = {0};
FILE *pFile1;
int a_pos = 0;
while ( fscanf( pFile1, "%[^:]: %d %d %d\n", Sports1[a_pos].country_name,
&Sports1[a_pos].gold, &Sports1[a_pos].bronze,
&Sports1[a_pos].silver ) != EOF ) {
++a_pos;
}
| |
| aaronljx is offline | |
| | #10 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > Oh, I don't know why you use fscanf == 4, perhaps to check whether you have scanned all the data, a better method i believe would be != EOF. Except that 4 is a positive assertion of success, not a negative assertion of complete failure. With a badly formatted input file, if fscanf gets stuck at the first conversion, it will enter an infinite loop of always returning 0. Never making progress, and never (eventually) reaching EOF.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
![]() |
| Tags |
| arrays, files, i/o, loop, string |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Multi dimensional array | $l4xklynx | C Programming | 7 | 01-03-2009 03:56 AM |
| Post... | maxorator | C++ Programming | 12 | 10-11-2005 08:39 AM |
| Dikumud | maxorator | C++ Programming | 1 | 10-01-2005 06:39 AM |
| Struct *** initialization | Saravanan | C Programming | 20 | 10-09-2003 12:04 PM |
| UNICODE and GET_STATE | Registered | C++ Programming | 1 | 07-15-2002 03:23 PM |