Trim Field of Spaces for Incoming Text File

Hello. I'm having a lot of trouble with trying to write the syntax for one of my codes. I need to: "Remove trailing spaces from fieldIn cstring and place in fieldOut cstring and if entire field is blank place one space followed by '\0'"

The main problem I'm having is calling the trimField() function

The trimFrield() function is called from my csvRecord transformDataToCSV() function, which is a structure type. Those are the variables being passed to trimField(). Here it what I have so far:

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

// For the required data members, Name, Id, Score, call trimField function to remove trailing spaces and place data into csvRecord, copy entire array
   // of answers to csvRecord and return record
csvRecord transformDataToCSV(Scantron &inputRecord){
    csvRecord outputRecord;
    trimField(inputRecord.Name[21], outputRecord.Name[21]);
    trimField(inputRecord.Id[11], outputRecord.Id[11]);
    trimField(inputRecord.score[4], outputRecord.score[4]);
    for(int i = 0; i < 60; ++i){
        outputRecord.answerArray[i] = inputRecord.answerArray[i];
        trimField(inputRecord.answerArray[61], outputRecord.answerArray[61]);
    }

    return outputRecord;
}


// Remove trailing spaces from fieldIn cstring and place in fieldOut cstring
// If entire field is blank place one space followed by '\0'
void trimField(char fieldIn[], char fieldOut[] )   {
    for(int i = 0; i < 150; i++)    {
        if(fieldIn[i] != ' ')   {
            strncpy(fieldIn, fieldOut, 150);
        }
    }
}



The file we are trimming looks like this. It includes extra variables but only certains ones are passed to fieldIn:
1
2
3
4
5
6
 1         2         3         4         5         6         7         8         9         0         
123456789012345678901234567890123456789012345678901345678901234567890123456789012345678901234567890123456
BROWN DANIELLA                 810805748            562431322143323235411221 4213414131344422312122      
DEREK SAMATHA I                                 00210011313243433233344312212321342123  14212224242131121
SIMPSON BRETT                                       3844342323232233 33412342212342113221331132411112132 
TOMATO A LUKE                  811327785 15328  003 4214341213331232354112432321532412124211142232213242 


Other instructor notes:
When you copied the input data into the cstrings in your struct you put a null (‘\0’) in the last position. You can search for the null (‘\0’) as explained above in the link on strrchr and you will get a pointer to the position in the cstring where the null was found. Now you can back up from there using pointer arithmetic (decrease the pointer by 1 to look at the previous position in the cstring) checking for the space character. If you find a space change it to a null. If you find a character your done trimming. If you keep finding spaces, eventually your pointer will be equal to the address of the cstring itself. When his happens you want to leave the space there so it will be copies into the output file.
Last edited on
If I had to guess, this:
trimField(inputRecord.Name[21], outputRecord.Name[21]);
should be
trimField(inputRecord.Name, outputRecord.Name);
but without knowing the internals of the Scantron class, it's just a guess.


strncpy(fieldIn, fieldOut, 150);
This will be called every time you find a space in a char array. Why? It will do the same thing ever time. Calling it ten times will be exactly the same as calling it once.
Last edited on
I want it to erase all the necessary spaces and replace it with one space. I'm not sure how to do that.

The structs I have defined are:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Scantron {
    char Name[21];
    char Id[11];
    char CRN[6];
    char testCode[3];
    char specialCode[4];
    char score[4];
    char answerArray[62] = " ";
};

struct csvRecord {
    char Name[21];
    char Id[11];
    char score[4];
    char answerArray[124] = " ";
};
Last edited on
Topic archived. No new replies allowed.