Jump to content
  • Advertisement
Sign in to follow this  
_Zido_

File reading/writing problem.

This topic is 4837 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, i'm writing a program in c that edits configuration files, for a paticular game server. The configuration files contains variables that are used by the actual server. I'll give you an example of what these files look like: // Rate at which exp. is given. base_exp_rate: 100 The row starting with "//" is a comment. The other row contains a variable and its value(variable: value). What my program does is asks the user to type the .conf file he/she wants to edit, then it asks the user to type the chosen variable followed by a space followed by the chosen value that he/she wants to edit. First, i made this program with no errors at all, but it didnt ask the user which file to change. Now when i added that feature, it seems to compile successfully, read the file successfully, but for some reason it wouldnt find the variable that the user chose. Heres the source: #include <stdio.h> #include <string.h> #include <stdlib.h> int readfile(char *filename,char *uservar,char *uservalue) { FILE* conffile; char temprow[1024]; char row[1024][1024]; char newrow[1024]; char var[512]; char value[512]; char *prow; int rown=0; int charn=0; int rowtotal=0; conffile = fopen(filename,"r"); while(fgets(temprow,1023,conffile)!=NULL) { prow=&row[rown][charn]; if(temprow[0]=='/' && temprow[1]=='/') { strcpy(prow,temprow); } else { sscanf(temprow,"%[^:]: %s",var,value); if (strcmp(var,uservar)==0) { snprintf(newrow,99,"%s: %s\n",var,uservalue); strcpy(prow,newrow); printf("%s",newrow); } else { strcpy(prow,temprow); } } printf("%s",prow); charn=0; rown++; rowtotal++; } charn=0; //Go back to char 1. rown = 0; //Go back to row 1. fclose(conffile); unlink(filename); conffile = fopen(filename,"w+"); while(rown<rowtotal) { prow=&row[rown][charn]; fprintf(conffile,"%s",prow); charn=0; rown++; } fclose(conffile); printf("File updated.\n"); return 0; } int main() { char ufilename[25]; char var[25]; char value[25]; printf("Please type the filename you wish to edit.\n"); scanf("%[^\n]",ufilename); system("cls"); printf("Please type your chosen variable, followed by a space, followed by your requested value.\n"); scanf("%24[^ ] %s",var,value); system("cls"); readbexp(ufilename,var,value); system("pause"); return 0; } All i need to do is make it work when the user chooses a file to edit. Thanks for you help, Zido.

Share this post


Link to post
Share on other sites
Advertisement
sourceified and readabilitified

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int readfile(char *filename,char *uservar,char *uservalue) {
FILE* conffile;

char temprow[1024];
char row[1024][1024];
char newrow[1024];
char var[512];
char value[512];

char *prow;

int rown=0;
int charn=0;
int rowtotal=0;

conffile = fopen(filename,"r");
while(fgets(temprow,1023,conffile)!=NULL) {
prow=&row[rown][charn];
if(temprow[0]=='/' && temprow[1]=='/') {
strcpy(prow,temprow);
}
else {
sscanf(temprow,"%[^:]: %s",var,value);
if (strcmp(var,uservar)==0) {
snprintf(newrow,99,"%s: %s\n",var,uservalue);
strcpy(prow,newrow);
printf("%s",newrow);
} else {
strcpy(prow,temprow);
}
}
printf("%s",prow);
charn=0;
rown++;
rowtotal++;
}
charn=0; //Go back to char 1.
rown = 0; //Go back to row 1.

fclose(conffile);

unlink(filename);
conffile = fopen(filename,"w+");
while(rown<rowtotal) {
prow=&row[rown][charn];
fprintf(conffile,"%s",prow);

charn=0;
rown++;
}

fclose(conffile);
printf("File updated.\n");

return 0;

}

int main() {
char ufilename[25];
char var[25];
char value[25];
printf("Please type the filename you wish to edit.\n");
scanf("%[^\n]",ufilename);
system("cls");
printf("Please type your chosen variable, followed by a space, followed by your requested value.\n");
scanf("%24[^ ] %s",var,value);
system("cls");
readbexp(ufilename,var,value);
system("pause");

return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by DavW
is the readfile function ever called?


No it's not he called readbexp(ufilename,var,value); instead of readfile(ufilename,var,value). Dunno how he got away with compile on that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!