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.

Recommended Answers

All 5 Replies

How to use fgetc() to read the file one charactger at a time. Note that chrLetter should be int not char because fgetc() returns int.

int chrLetter;
while( (chrLetter = fgetc( pfilInput ) != EOF)
{
   strBuffer[Index++] = chrLetter;
}

If you are not required to use fgetc() then I'd suggest fgets()

while( fgets(strBuffer,sizeof(strBuffer), pfilInput) )
{
   // blabla
}

Ok, so no I have this:

int main()
{
    // Declare a file pointer
    FILE *pfilInput = 0;
    bool blnResult = false;
    char strBuffer[ 150 ];
    char chrLetter = 0;
    int intIndex = 0;
    char strField[50];
    char strDestination[150];

    // 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 )
    {

        while( fgets(strBuffer, sizeof(strBuffer), pfilInput))
        {
            strDestination[intIndex] = strBuffer[intIndex];
            int intDestinationLength = strlen(strDestination);

            for(intIndex = 0; intIndex < intDestinationLength; intIndex += 1)
            {
                if(strDestination[intIndex] == ',')
                {

                }
            }
        }
        fclose( pfilInput );
    }

    system("pause");
}

I need to get each line, separate each comma delimited field into the appropriate structure
variable, then go to the next line, and do it again until eof.
Once I start looking for the comma delimiter, I don't know to do after that.
I can't use any built in string functions, or pointers.

what is line 21 supposed to do? What it actually does is copy the contents of a single character from one buffer to another. There is no need to copy the input buffer, just use it directly in the remaining code.

If think you will have to write two functions that duplicate standard C library functions. They will be needed so that you can extract the fields from the line just read and put the values in the structure. They would be easier for you to write if you wrote a small test program that just tests the function, that way you can get the algorithm down right before putting the code in your final program.

  1. atoi() -- to convert string to integer
  2. strtok() -- extract a substring that is terminated by a given character such as a comma.

I started writing the algorithm to mimic strtok as you suggested.
I AM allowed to use the atoi function.
Here is what I have so far.

int main()
{
    // Declare a file pointer
    FILE *pfilInput = 0;
    bool blnResult = false;
//  char strBuffer[ 150 ];
    char chrLetter = 0;
    int intIndex = 0;
    char strField[100];
    char chrDelimiter = ',';
    int intFieldIndex = 0;
    char strBuffer2[] = "21, Andres M. Quesada, 848 Patterson St., Newport, KY, 41071";
    udtAddressType udtAddress[100];
    int intFieldLength = 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 )
    {

        while( fgets(strBuffer, sizeof(strBuffer), pfilInput))
        {
            while(strBuffer[intIndex] != chrDelimiter)
            {
            strField[intFieldIndex] = strBuffer[intIndex];
            intIndex += 1;
            intFieldIndex += 1;
            }

        }
        fclose( pfilInput );
    }*/

    while(strBuffer2[intIndex] != chrDelimiter)
    {
        strField[intFieldIndex] = strBuffer2[intIndex];
        intIndex += 1;
        intFieldIndex += 1;
    }

    strField[intFieldIndex] = '\0';
    intFieldIndex = 0;
    udtAddress[0].lngRecordId = atoi(strField);
    printf("The record ID is: %ld \n", udtAddress[0].lngRecordId); //Check to make sure parsing worked

    //Need plus 1 to skip over delimiter
    while(strBuffer2[intIndex + 1] != chrDelimiter) 
    {
    //need the plus 1 here to skip over the delimiter
        strField[intFieldIndex] = strBuffer2[intIndex + 1]; 
        intIndex += 1;
        intFieldIndex += 1;
    }

    strField[intFieldIndex] = '\0';
    printf("Your field is: %s\n", strField); //Checking to make sure field 2 parsed in correctly
    intFieldIndex = 0;
    intFieldLength = strlen(strField);
    printf("Your field length is: %d \n", intFieldLength);
    printf("Your field is: %s\n", strField); //Checking to make sure field 2 parsed in correctly

    for(intFieldIndex = 0; intFieldIndex < intFieldLength; intFieldIndex += 1)
    {
        udtAddress[0].strFullName[intFieldIndex] = strField[intFieldIndex];
    }

    udtAddress[0].strFullName[intFieldIndex] = 0;
    printf("Your full name is: %s \n", udtAddress[0].strFullName);

    while(strBuffer2[intIndex + 1] != chrDelimiter)
    {
        strField[intFieldIndex] = strBuffer2[intIndex + 1];
        intIndex += 1;
        intFieldIndex += 1;
    }

    system("pause");
}

Now I want to start breaking up my parsing while loops into procedures.
Using what I have so far, I would have to pass intIndex, strBuffer2, and the chrDelimiter.
I want to return strField, so I can used that to popluate the structures, but I can't
find any information and how to return a string without using pointers.
Is there a way to return a string without using pointers, or maybe another method using what I have done so far?

I can't find any information and how to return a string without using pointers.

So use pointers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.