![]() |
| | #1 |
| Registered User Join Date: Nov 2007
Posts: 99
| String value into a int vector please help me, what i am trying is i am reading a file and strtok on newline and sending those lines to a string vector then the contents of the file contain numbers like 12356 .... so i want to send these character by character from string vector to a int vector so at last i want al those values in the int vector how to achieve this can you please help me thanks in advance. |
| vin_pll is offline | |
| | #2 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 12,704
| I guess that by vector you mean dynamic array? From what you describe, it may be easier to use getchar() instead of strtok().
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is online now | |
| | #3 |
| and the Rod of Remorse Join Date: Apr 2006 Location: United States
Posts: 3,741
| Laserlight's right. Here, watch this execute; while far from ideal, it should give you some ideas. Code: #include <stdio.h>
#include <ctype.h>
int main(void)
{
FILE *infile = stdin; /* read from keyboard */
int vector[255];
int elem;
size_t count;
const size_t N = sizeof vector / sizeof vector[0];
for ( count = 0; count < N; ) {
elem = fgetc(infile);
if ( elem != EOF ) {
if ( isdigit(elem) ) {
vector[count] = (int)( elem - '0' );
printf("vector[%d] = %d\n" , count , vector[count]);
count++;
}
}
else {
break;
}
}
return 0;
}
__________________ <Padathir> Like the guy said, he is a clown, doing silly things for his own and other's amusement --------- What undefined behavior is like. Last edited by whiteflags; 11-21-2009 at 05:00 AM. |
| whiteflags is offline | |
| | #4 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 12,704
| Oops, yes, getchar() reads from stdin, whereas this is supposed to read from file.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is online now | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to combine these working parts?? | transgalactic2 | C Programming | 0 | 02-01-2009 08:19 AM |
| can some one please tell me the cause of the error ? | broli86 | C Programming | 8 | 06-26-2008 08:36 PM |
| My graphics library | stupid_mutt | C Programming | 3 | 11-26-2001 06:05 PM |
| How do you search & sort an array? | sketchit | C Programming | 30 | 11-03-2001 05:26 PM |
| A Simple (?) Problem | Unregistered | C++ Programming | 8 | 10-12-2001 04:28 AM |