For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > May 2006 > Segmentation fault









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Segmentation fault
didier2309

2006-05-15, 7:08 pm

I get segmentation fault when I enter that function it seems to work
well until I reach wcpcpy:

int acopen::open_acc()
{
int accnum;
char stname[80];
FILE *getaccnum;
getaccnum = fopen("numbers", "r");
if(getaccnum == NULL){
cout << "No account has been openned yet\n";
wcpcpy(lacc_num, L"000-00-0001");
}
else{
fs(getaccnum, 11, SEEK_END);
fscanf(getaccnum, "%S", lacc_num);
int sec[9], accnum;
sec[0] = wctob(lacc_num[0]);
sec[1] = wctob(lacc_num[1]);
sec[2] = wctob(lacc_num[2]);
sec[3] = wctob(lacc_num[4]);
sec[4] = wctob(lacc_num[5]);
sec[5] = wctob(lacc_num[7]);
sec[6] = wctob(lacc_num[8]);
sec[7] = wctob(lacc_num[9]);
sec[8] = wctob(lacc_num[10]);
for(int i = 0; i < 9; i--){
accnum = accnum*10 +sec[i] ;
}

int tempaccnum;

accnum = accnum++;
tempaccnum = accnum;
for(int i = 8; i >= 0; i--){
sec[i] = tempaccnum - (tempaccnum/10)*10;
tempaccnum = tempaccnum/10;
}
lacc_num[0] = btowc(sec[0]);
lacc_num[1] = btowc(sec[1]);
lacc_num[2] = btowc(sec[2]);
lacc_num[3] = '-';
lacc_num[4] = btowc(sec[3]);
lacc_num[5] = btowc(sec[4]);
lacc_num[6] = '-';
lacc_num[7] = btowc(sec[5]);
lacc_num[8] = btowc(sec[6]);
lacc_num[9] = btowc(sec[7]);
lacc_num[10] = btowc(sec[8]);
lacc_num[11] = '\0';

}
fclose(getaccnum);
FILE *putn;
putn = fopen("numbers","ab");
if(putn == NULL){
cout << "\ncannnot open file\n";
return 1;
}
FILE *out;
out = fopen("account","ab");
if(out == NULL) {
cout << "\ncannot open file";
return 1;
}


cout <<"\nthe account number is: ";
printf("%S\n",lacc_num);
cout << "enter name: \n";
cin >> sname;

cout << "enter last name\n";
cin >> slastname;

cout << "birthday? Day, month and year seperately (in
numbers)...\n";
do{
cin >> sbirth_day ;
if(!getint(sbirth_day))
badent();//call badebt()
}while(!getint(sbirth_day));

do{
cin >> sbirth_month ;
if(!getint(sbirth_month))
badent();
}while(!getint(sbirth_month));

do{
cin >> sbirth_year;
if(!getint(sbirth_year))
badent();
}while(!getint(sbirth_year));

cout << "street address?(house number and street name only:
Enter [space]~ to end address) \n";
cin >> shousenum ;
do{
cin >> stname;
strcat(sstreetname, stname);
strcat(sstreetname, " ");
}while(strcmp(stname,"~"));


cout << "city?\n";
cin >>scity;

do{
cout <<"zipcode?\n";
cin >> szip;
}while(!getlong(szip));

cout << "phone and area code?\n";
do{
cin >> sphone;
if(!getlong(sphone))
badent();
}while(!getlong(sphone));

do{
cin >> sarea ;
if(!getint(sarea))
badent();
}while(!getint(sarea));

do{
cout << "social security?\n";
cin >>ssocial;
}while(!getlong(ssocial));

do{
cout << "deposit amount?\n";
cin >>sbalance;
}while(!(getfloat(sbalance)));
fprintf(out, "%S\n", lacc_num);
fprintf(putn, "%S\n", lacc_num);
fprintf(out ,"%s\n", sname);
fprintf(out ,"%s\n", slastname);
fprintf(out, "%s\n", sbirth_day);
fprintf(out, "%s\n", sbirth_month);
fprintf(out, "%s\n", sbirth_year);
fprintf(out, "%s\n", sstreetname);
fprintf(out, "%s\n", shousenum);
fprintf(out, "%s\n", scity);
fprintf(out, "%s\n", szip);
fprintf(out, "%s\n", sphone);
fprintf(out, "%s\n", sarea);
fprintf(out, "%s\n", ssocial);
fprintf(out, "%s\n", sbalance);
fclose(putn);
fclose(out);
return 0;
}

Tell me what could be wrong.
Thank you

Gordon Burditt

2006-05-15, 7:08 pm

>I get segmentation fault when I enter that function it seems to work
>well until I reach wcpcpy:


If getaccnum == NULL, don't you think it's a bad idea to
fclose(getaccnum), which you do right after the call to wcpcpy
(after the else clause on the if)?

You are passing the wrong type arguments to wcpcpy(), which should
a pointer to the authorization to copy a Weapons Control Protocol
package and the deployment orders. The problem is likely in line
42 of the source code of wcpcpy() which you forgot to post.

Gordon L. Burditt

>
>int acopen::open_acc()
>{
> int accnum;
> char stname[80];
> FILE *getaccnum;
> getaccnum = fopen("numbers", "r");
> if(getaccnum == NULL){
> cout << "No account has been openned yet\n";
> wcpcpy(lacc_num, L"000-00-0001");
> }
> else{
> fs(getaccnum, 11, SEEK_END);
> fscanf(getaccnum, "%S", lacc_num);
> int sec[9], accnum;
> sec[0] = wctob(lacc_num[0]);
> sec[1] = wctob(lacc_num[1]);
> sec[2] = wctob(lacc_num[2]);
> sec[3] = wctob(lacc_num[4]);
> sec[4] = wctob(lacc_num[5]);
> sec[5] = wctob(lacc_num[7]);
> sec[6] = wctob(lacc_num[8]);
> sec[7] = wctob(lacc_num[9]);
> sec[8] = wctob(lacc_num[10]);
> for(int i = 0; i < 9; i--){
> accnum = accnum*10 +sec[i] ;
> }
>
> int tempaccnum;
>
> accnum = accnum++;
> tempaccnum = accnum;
> for(int i = 8; i >= 0; i--){
> sec[i] = tempaccnum - (tempaccnum/10)*10;
> tempaccnum = tempaccnum/10;
> }
> lacc_num[0] = btowc(sec[0]);
> lacc_num[1] = btowc(sec[1]);
> lacc_num[2] = btowc(sec[2]);
> lacc_num[3] = '-';
> lacc_num[4] = btowc(sec[3]);
> lacc_num[5] = btowc(sec[4]);
> lacc_num[6] = '-';
> lacc_num[7] = btowc(sec[5]);
> lacc_num[8] = btowc(sec[6]);
> lacc_num[9] = btowc(sec[7]);
> lacc_num[10] = btowc(sec[8]);
> lacc_num[11] = '\0';
>
> }
> fclose(getaccnum);
> FILE *putn;
> putn = fopen("numbers","ab");
> if(putn == NULL){
> cout << "\ncannnot open file\n";
> return 1;
> }
> FILE *out;
> out = fopen("account","ab");
> if(out == NULL) {
> cout << "\ncannot open file";
> return 1;
> }
>
>
> cout <<"\nthe account number is: ";
> printf("%S\n",lacc_num);
> cout << "enter name: \n";
> cin >> sname;
>
> cout << "enter last name\n";
> cin >> slastname;
>
> cout << "birthday? Day, month and year seperately (in
>numbers)...\n";
> do{
> cin >> sbirth_day ;
> if(!getint(sbirth_day))
> badent();//call badebt()
> }while(!getint(sbirth_day));
>
> do{
> cin >> sbirth_month ;
> if(!getint(sbirth_month))
> badent();
> }while(!getint(sbirth_month));
>
> do{
> cin >> sbirth_year;
> if(!getint(sbirth_year))
> badent();
> }while(!getint(sbirth_year));
>
> cout << "street address?(house number and street name only:
>Enter [space]~ to end address) \n";
> cin >> shousenum ;
> do{
> cin >> stname;
> strcat(sstreetname, stname);
> strcat(sstreetname, " ");
> }while(strcmp(stname,"~"));
>
>
> cout << "city?\n";
> cin >>scity;
>
> do{
> cout <<"zipcode?\n";
> cin >> szip;
> }while(!getlong(szip));
>
> cout << "phone and area code?\n";
> do{
> cin >> sphone;
> if(!getlong(sphone))
> badent();
> }while(!getlong(sphone));
>
> do{
> cin >> sarea ;
> if(!getint(sarea))
> badent();
> }while(!getint(sarea));
>
> do{
> cout << "social security?\n";
> cin >>ssocial;
> }while(!getlong(ssocial));
>
> do{
> cout << "deposit amount?\n";
> cin >>sbalance;
> }while(!(getfloat(sbalance)));
> fprintf(out, "%S\n", lacc_num);
> fprintf(putn, "%S\n", lacc_num);
> fprintf(out ,"%s\n", sname);
> fprintf(out ,"%s\n", slastname);
> fprintf(out, "%s\n", sbirth_day);
> fprintf(out, "%s\n", sbirth_month);
> fprintf(out, "%s\n", sbirth_year);
> fprintf(out, "%s\n", sstreetname);
> fprintf(out, "%s\n", shousenum);
> fprintf(out, "%s\n", scity);
> fprintf(out, "%s\n", szip);
> fprintf(out, "%s\n", sphone);
> fprintf(out, "%s\n", sarea);
> fprintf(out, "%s\n", ssocial);
> fprintf(out, "%s\n", sbalance);
> fclose(putn);
> fclose(out);
> return 0;
>}
>
>Tell me what could be wrong.
>Thank you
>



didier2309

2006-05-15, 7:08 pm

Isn't lacc_num without the braces a pointer to an array? The array is
of type wchar_t. I'tt ry to find the library file which contain the
wchar_t.

didier2309

2006-05-19, 7:03 pm

I think there may be to much memory consumption and the char* itoa(int
i); function is deprecated, do you know any other way I can do this?

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com