Advertisement

reading from a file

Started by May 16, 2002 07:56 PM
19 comments, last by gamefr 22 years, 6 months ago
Compile this code as a console app and call with one or more arguments like "Name=", "Gender=", and "Age=". This code should give you a base to work from. Of course its very simplistic, it has no error checking and I''ve used a hardcoded text string rather than loading from a file, but the core bits are there.


,Jay


  // FileTags.cpp : Defines the entry point for the console application.////#include "stdafx.h" //I''ve commented this out - unneccessary for example#include <stdio.h>#include <string.h>int Terminator = '';'';int main(int argc, char* argv[]){	int iTagCounter = 1;	char *strTextFile = "Name=Yoda; Age=900; Gender=Undetermined;";	char strParams[80] = "";	char *strStart = NULL;	char *strStop = NULL;	int iCharsToCopy = 0;	//Load the text into strFileText.	do 	{		strStart = strstr(strTextFile,argv[iTagCounter]); // Position of Tag within String		strStart += strlen(argv[iTagCounter]); // Advance start pointer to end of tag		strStop = strchr(strStart,Terminator); // Position of first terminator after Tag		iCharsToCopy = strStop - strStart; // Number of Characters from Tag to Terminator		strncat(strParams,strStart,iCharsToCopy); // Copy N chars from strStart		printf("Tag = ''%s'' \n",argv[iTagCounter]); // Output Tag		printf("Parameter = ''%s'' \n",strParams); // Output Parameter		iTagCounter++; //Move onto next Tag	} while (argc<iTagCounter);		return 0;}  





This topic is closed to new replies.

Advertisement