Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help converting c=fgetc to integer

Status
Not open for further replies.

87vette

Programmer
Mar 4, 2002
9
US
i am using fgetc to read from a file.also my project uses a double link list here is a sample of my program c=fgetc here is the rest

else if (isdigit(c))//this use to delete from the link list
{
//delete();
//del=atoi(c);

//delNum=c;
if(del==0)
continue;
//del=atoi(delNum);
currptr=headptr;
delcount=1;
while(delcount!=del&&currptr!=NULL)
{
currptr=currptr->next;
delcount++;
}
if(currptr==NULL)
continue;
else if(currptr==headptr)
{
headptr=currptr->next;
headptr->prev=NULL;
temp=currptr;
}
else if(currptr==tailptr)
{
tailptr=currptr->prev;
temp=currptr;
tailptr->next=NULL; Nail the code
 
I don't get the question. May you please tell me what you really want. May be a direct and unambigous(sp) post would help.
 
I need to convert c to an integer that was i know which link to delete from the list. for example if i read a 4 from the file then i delete the 4th link from the list. i am trying to use atoi with c but i get an error.is there any other way i can parse the char c to an int? yes c is declared as char c. Nail the code
 
Declare c an del as integers. Then try this:
Code:
      del = c - 48;
OR this:
Code:
      del = c & 0xf;
to change the character c to an integer between 0 and 9.
 
hmmmm......... the above solution works only if there are less than 10 links!!!!
If your program reads an integer greater than 9 than it is a potential bug!!!

What I suggest is decare c as a charcter array. Then u can use atoi()

NOTE: atoi expects a character array (vector variable). Not a scalar variable. Hence the error in ur case.

The solution is....

char strInt[n] where n > 1 (actualy depends upon how how many characters the integer is made of. Also... reserve 1 space for the NULL character.

Next... read the value from the file.
then

del = atoi(strInt);

This will work.

cheers
 
how about if i would use this instead atoi(int(c)) do you guys think this would work. i am at work right now but i am
going to work on my program when i get home. the reason that i cannot declare c as integer is because i am also reading alphas from the files. i could do it all in hex but that means i will have to change my whole program thanks for everyones help. if you have any other suggestions this will be greatly appreciated Nail the code
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top