Hi, New to C programming
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);
I get Tons of I's on the screen when i run this program in VS2008 , and
: 0 0 0
: 0 0 0
.......
when i run it using the linux cc compiler. Please help,