My assignmnet is to read a csv into a structure.
A few sample lines of the csv look like this:
21, John Henry Blue, 6598 Wish Street, Elves, CA, 54862
9, Chad Vile, 32 Bundle St. , Carver, IN, 28105
I can't use pointers, except to point to the file to be read in.
I can't use any built in string functions, I have to use the ones I wrote myself.
I'm stuck at the start, how to use fgetc() to get the first line of characters into an array so i can manipulate it.
Here is what I have so far.
//-------------------------------------------------------------------------------
//Includes
//-------------------------------------------------------------------------------
#include <stdio.h>
#include <iostream>
using namespace std;
//-------------------------------------------------------------------------------
//User Defined Types
//-------------------------------------------------------------------------------
typedef struct udtAddress
{
long lngRecordId;
char strFullName[100];
char strFirstName[50];
char strMiddleName[50];
char strLastName[50];
char strStreet[100];
char strCity[50];
char strState[50];
char strZipCode[50];
}udtAddressType;
//-------------------------------------------------------------------------------
//Prototypes
//-------------------------------------------------------------------------------
bool OpenInputFile(char strFileName [], FILE *&pFileInput);
//-------------------------------------------------------------------------------
//Start Main Program
//-------------------------------------------------------------------------------
int main()
{
// Declare a file pointer
FILE *pfilInput = 0;
bool blnResult = false;
char strBuffer[ 150 ];
char chrLetter = 0;
int intIndex = 0;
// Try to open the file for reading
blnResult = OpenInputFile( "C:\\Users\\AndresQuesada\\Dropbox\\C ++\\Homework4Structures\\Homework4Addresses\\Addresses1.txt", pfilInput);
// Was the file opened?
if( blnResult == true )
{
// Yes, read in records until end of file( EOF )
while( feof( pfilInput ) == false )
{
// How to read one character at a time
chrLetter = fgetc( pfilInput );
while(chrLetter != '\r' || chrLetter != '\n')
{
strBuffer[intIndex] = chrLetter;
intIndex += 1;
}
printf("%s", strBuffer);
// How to read one line at a time
//fgets( strBuffer, sizeof( strBuffer ), pfilInput );
// Print out line to screen
//printf( "%s\n", strBuffer );
}
// Clean up
fclose( pfilInput );
}
system("pause");
}
bool OpenInputFile( char strFileName[ ], FILE *&pfilInput )
{
bool blnResult = false;
// Open the file for reading
pfilInput = fopen( strFileName, "rb" );
// Success?
if( pfilInput != 0 )
{
// Yes
blnResult = true;
}
else
{
// No
printf( "Error opening %s for reading!!!\n", strFileName );
}
return blnResult;
}
At the moment, all I want help with is getting line 47 working. I want to get my first line of chars
into strBuffer so I can manipulate it.