The Elina
The Elina

Reputation: 19

Why does my c code not add the correct null zero at the end like it is supposed to and keeps printing out code?

I do not know why my code does not seem to be working properly. I am reading from a file, and grabbing each line and from there I am using my own function to try and break down each of the lines and add them to character arrays in a structure and then add those structures to an array. But for whatever reason, when I am trying to indivudually print out the individual values for all of the information it keeps printing out all of it. From what I am seeing, for whatever reason even though my function strsub is supposed to add a '\0' at the end, it does not seem to be doing that. So every time I pass in the pointer to the begging of each of the character variables it does not stop until the end of the whole structure so it starts by printing out the whole string and then prints out less and less. Is that the problem that I really have or am I missing something else?

This is my code so far. I first just tried creating a struct and filling the array with each pass, but unfortunantly I had the same issue.

#define _CRT_SECURE_NO_WARNINGS // Since I want to strictly use ANSI C and not Microsoft C without getting the warning message, I'm adding this line of code before I include header files.
#include <stdio.h>              // "#include" includes the contents of another file, commonly called header file, into the source code file.
#include <string.h>
#define MAX 100
FILE *fp, *csit;

void strsub(char buf[], char sub[], int start, int end);
void printArray(struct trainCartrain[]);

struct trainCar {
    char car[10];
    char type[2];
    char weight[6];
    char length[3];
    char horsepower[3];
    char numberInTrain[4];
};

int main() {

    struct trainCar ar[7];
    struct trainCar train;

    // test and open input file and output file.; 
    
    if (!(fp = fopen("train.txt", "r"))) {
        printf("train.txt could not be opened for input.");
        exit(1);
    }
    if (!(csit = fopen("csit.txt", "w"))) {
        printf("csit.txt could not be opened for output.");
        exit(1);
    }
 
    int i = 0;
    char buf[MAX];

    while (!feof(fp)) {
        fgets(buf, MAX, fp);
        strsub(buf, train.car, 0, 9);
        strsub(buf, train.type, 10, 11);
        strsub(buf, train.weight, 12, 17);
        strsub(buf, train.length, 18, 20);
        strsub(buf, train.horsepower, 21, 23);
        strsub(buf, train.numberInTrain, 24, 27);
        
        printf("%s", train.car);
        printf("%s", train.type);
        ar[i] = train;
        i++;
    }

    printArray(ar);

    fclose(csit);
    fclose(fp);
    return 0;
}
void strsub(char buf[], char sub[], int start, int end) { //strsub () grabs a substring, sub, from a string, buf, given the start and end index within the string.
    int i, j;
    for (j = 0, i = start; i <= end; i++, j++) {
        sub[j] = buf[i];
    }
    sub[j] = '\0'; 
    //end with the null terminator character that signifies the end of a string.
}

My file is small and simple, textfile


Boxcar    D 44000 55 16 45
Hopper    B 23000 62 18 33
Tanker    G 15000 45 30 12
Autocar   A 30000 37 23 6
Livestock L 56500 50 18 19
Coalcar   C 49300 53 22 100
Flatcar   F 18000 66 15 25

and what it prints out is

Boxcar    D 44000 55 16 45
D 44000 55 16 45
44000 55 16 45
55 16 45
16 45
45
Hopper    B 23000 62 18 33
B 23000 62 18 33
23000 62 18 33
62 18 33
18 33
33
Tanker    G 15000 45 30 12
G 15000 45 30 12
15000 45 30 12
45 30 12
30 12
12
Autocar   A 30000 37 23 6
A 30000 37 23 6
30000 37 23 6
37 23 6
23 6
6
Livestock L 56500 50 18 19
L 56500 50 18 19
56500 50 18 19
50 18 19
18 19
19
Coalcar   C 49300 53 22 100
Flatcar   F 18000 66 15 25C 49300 53 22 100
Flatcar   F 18000 66 15 2549300 53 22 100
Flatcar   F 18000 66 15 2553 22 100
Flatcar   F 18000 66 15 2522 100
Flatcar   F 18000 66 15 25100
Flatcar   F 18000 66 15 25Flatcar   F 18000 66 15 25F 18000 66 15 2518000 66 15 2566 15 2515 2525

can someone please explain what I am doing wrong? I do have to use this function strsub for my class too.

I am just trying to get it to print out the individual charachter data and not the whole string each time. I think it is an issue with the terminating zero at the end and when I tried debugging it does not seem to be adding that for some reason. I don't know why though, if that is the problem.

Upvotes: 0

Views: 52

Answers (1)

mch
mch

Reputation: 9804

strsub(buf, train.car, 0, 9); accesses train.car with index 0 till 9 in the loop and then index 10 outside, but that's already out of bounds for a char car[10];.

Solution:

Increase the size of all of your arrays by 1 to have space for the 0-terminator of the string.

Also have a look at Why is “while( !feof(file) )” always wrong? . It is not related to your problem, but you might run into that problem in the next minutes.

Instead of

while (!feof(fp)) {
    fgets(buf, MAX, fp);
    ....
}

use

while (fgets(buf, MAX, fp)) {
    ....
}

You missed a space in void printArray(struct trainCartrain[]);. It should be void printArray(struct trainCar train[]); and moved to after the definition of struct trainCar.

You also have to #include <stdlib.h> to use exit(1);

Upvotes: 3

Related Questions