سؤال

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>


#define MAX_KEYS 26
#define MAX_UID 4

struct Key {
    int keynum;
    int id;
    char cryptkeys[65];
};



int main(int argc, char ** argv) {
int MAX_LINE = 69;
struct Key *table = malloc(MAX_UID * sizeof(struct Key));

FILE *fileopen = fopen(filename, "r");
char *a[8];
int size = 0;
char *e;
char *string = NULL;
//reading the file
if(fileopen) {
        while((e =  fgets( line, MAX_LINE, fileopen )) != NULL) {
            printf(e);
            a[size] = strdup(e);
            size++;
        }

}
else {
    printf("File does not exist\n");
    exit(0);
}
for(int i = 0; i < size; i++ ) {
    //printf("%s", a[i]); //no zero showing up

}
//parsing the id and the keys
char id[3];
char key[65];
int ids;
int idnum;
for(int i = 0; i < size-1; i++) {
    struct Key *k = (struct Key*)malloc(sizeof(struct Key));
    string = a[i];
    //set the ID
    for(int ix = 0; ix < 3; ix++) {
        id[ix] = string[ix];
    }
    //printf("%s", id); No zero inbetween ID
    //set the Keys
    for(int j = 4; j < 68; j++) {
        key[j-4] = string[j];
    }
    ids = strtol(string, &id2, 10);
    //printf("Id is %d", ids); //zero shows up here
    //printf("   ");
    k->id = ids;
    strcpy(k->cryptkeys, key);
    k->keynum++;
    table[i] = *k;
    idnum++;
}
for(int i = 0; i < 5; i++) {
    printf("%d", table[i].id);
    printf(" ");
    printf("%s", table[i].cryptkeys);
    printf("\n");

}
return 0;
}

Hey guys, I'm trying to work on struct manipulation inside array of pointers to my structs. I can add the values just fine, but I keep getting this 0 appearing between my lines.My file is only three lines long with an integer and a string afterwards. Everything seems to parse correctly but a zero keeps showing up in my text.

My file looks like this in case you were wondering

421 0123456789abcdef0123456789abcde00123456789abcdef0123456789abcde0

422 0000000000000000000000000000000000000000000000000000000000000000

423 1111111111111111111111111111111111111111111111111111111111111111

and when I try to print the output after reading in my table it looks like this.

421 0123456789abcdef0123456789abcde00123456789abcdef0123456789abcde0

0

422 0000000000000000000000000000000000000000000000000000000000000000

0

423 1111111111111111111111111111111111111111111111111111111111111111

I dont think it is coming from the back of the strings because each key is the appropriate length. Any suggestions on what it might be coming from? Any help would be greatly appreciated.

هل كانت مفيدة؟

المحلول

The function fgets will read line in string line until MAX_LINE characters are read or it hits new line. Hence you can first allocate line string with sufficient memory to read line. In this case it is MAX_LINE .

char line[MAX_LINE];
char *a[8];
while((e =  fgets( line, MAX_LINE, fileopen )) != NULL) 
{
   printf(line);
   a[size] = strdup(line);
   size++;
 }

نصائح أخرى

The main problem is that you use strcpy(k->cryptkeys, key); where key is not null-terminated. You only write to the first 64 elements and leave the last one uninitialized.

But there are some general issues as well, e.g. you're leaking memory because you call malloc in the loop without freeing (you could write the program just fine without using malloc at all) and you're unneccessarily reading the whole file into memory.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top