| | |
string to integer array transformation
![]() |
•
•
Join Date: Sep 2003
Posts: 81
Reputation:
Solved Threads: 0
Hello,
Ok.. here is another problem I have come up with..
I have a string of numbers (comma or '\n' separated (from .csv file)). I have allocated an array of integers to put in the numbers into yet how do I actually do it.
Example: I have "13, 34, 23, 12\n" string and my program should transform it into a[0]=13; a[2]=34; a[3]=23; a[4]=12
______________________________________________________________
Here are some of my ideas:
For transforming I am trying to use sscanf();
For finding what the number is (in char array format) I use a false code (works with more error then correct):
bool spc = false;
char numb[10];
memset (numb, ' ', sizeof(char)*(10+4));
printf ("\nnumbers: ");
for (int i=0; data[i]!='\0'; i++)
{
if (data[i]=='\n')
spc=true;
if (spc==true && (nch(data[i])==true || data[i]=='-'))
{
int a=i;
for (int b=0; b<10; a++, b++)
{
if ((nch(data[i])==true || data[i]=='-') && (data[i]!='\n'))
{
numb[b] = data[a];
if (data[a]=='\n')
break;
if (data[a]=='\n')
printf ("\n\\n incountered");
}
}
i=a;
fflush (stdout);
printf ("%s :: ",numb);
}
}
}
Ok.. here is another problem I have come up with..
I have a string of numbers (comma or '\n' separated (from .csv file)). I have allocated an array of integers to put in the numbers into yet how do I actually do it.
Example: I have "13, 34, 23, 12\n" string and my program should transform it into a[0]=13; a[2]=34; a[3]=23; a[4]=12
______________________________________________________________
Here are some of my ideas:
For transforming I am trying to use sscanf();
For finding what the number is (in char array format) I use a false code (works with more error then correct):
bool spc = false;
char numb[10];
memset (numb, ' ', sizeof(char)*(10+4));
printf ("\nnumbers: ");
for (int i=0; data[i]!='\0'; i++)
{
if (data[i]=='\n')
spc=true;
if (spc==true && (nch(data[i])==true || data[i]=='-'))
{
int a=i;
for (int b=0; b<10; a++, b++)
{
if ((nch(data[i])==true || data[i]=='-') && (data[i]!='\n'))
{
numb[b] = data[a];
if (data[a]=='\n')
break;
if (data[a]=='\n')
printf ("\n\\n incountered");
}
}
i=a;
fflush (stdout);
printf ("%s :: ",numb);
}
}
}
•
•
Join Date: Sep 2003
Posts: 81
Reputation:
Solved Threads: 0
I think I did it...
______________________________________________________
//ch4n is part of this class function that retuns 1 if the char is a number or -1 //and 0 otherwise
short sn=0;
int f=0, g=0;
for (int i = 0; i<strlen(data); i++)
{
if (data[i]=='\n')
sn=1;
if (ch4n(data[i])==1 && sn==1)
{
char numb[10];
memset (numb, ' ', sizeof(numb));
int b = 0;
for (int a=i; ch4n(data[a])==1; a++)
{
numb[b]=data[a];
b++;
i=a+1;
}
sscanf (numb,"%d",&arrays.ar[f].values[g]);
fflush (stdout);
f++;
if (f==arrays.width)
{
f=0;
g++;
}
}
}
______________________________________________________
//ch4n is part of this class function that retuns 1 if the char is a number or -1 //and 0 otherwise
short sn=0;
int f=0, g=0;
for (int i = 0; i<strlen(data); i++)
{
if (data[i]=='\n')
sn=1;
if (ch4n(data[i])==1 && sn==1)
{
char numb[10];
memset (numb, ' ', sizeof(numb));
int b = 0;
for (int a=i; ch4n(data[a])==1; a++)
{
numb[b]=data[a];
b++;
i=a+1;
}
sscanf (numb,"%d",&arrays.ar[f].values[g]);
fflush (stdout);
f++;
if (f==arrays.width)
{
f=0;
g++;
}
}
}
![]() |
Similar Threads
- Problems from string to int array (C)
- Coverting a string to a byte array (Visual Basic 4 / 5 / 6)
- Converting a String into an integer array (C)
- String to integer to ascii (C)
Other Threads in the C Forum
- Previous Thread: file input and output
- Next Thread: printf buffer strange behaviur
| Thread Tools | Search this Thread |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o inches include infiniteloop input intmain() iso keyboard km linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi





