C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-21-2009, 03:32 AM   #1
Registered User
 
Join Date: Nov 2007
Posts: 99
String value into a int vector

Dear Experts,

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   Reply With Quote
Old 11-21-2009, 03:42 AM   #2
C++ Witch
 
laserlight's Avatar
 
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   Reply With Quote
Old 11-21-2009, 04:49 AM   #3
and the Rod of Remorse
 
whiteflags's Avatar
 
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   Reply With Quote
Old 11-21-2009, 05:09 AM   #4
C++ Witch
 
laserlight's Avatar
 
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:31 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22