Pergunta

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

struct fileData
{
        char fileName[100];
        int size;
        char type;
        long timestamp;
};

void print(struct fileData *myFile);

int main(void)
{
        struct fileData *toUse = malloc(sizeof(toUse));
        char temp[100] = {0};

        printf("Enter the type:");
        getchar();
        scanf("%c", toUse->type);
        getchar();
        printf("Enter the filename:");
        fgets(temp, 100, stdin);
        strcpy(toUse->fileName, temp);
        printf("Enter the access time:");
        scanf("%lf", toUse->timestamp);
        printf("Enter the size:");
        scanf("%d", toUse->size);

        print(toUse);
        free(toUse);
}

void print(struct fileData *myFile)
{
        printf("Filename: %s - Size: %d - Type: [%c] - Accessed @ %ld\n", myFile->fileName, myFile->size, myFile->type, myFile->timestamp);
}

Ok so here is what the program is supposed to do:

  1. Make a structure for data of a file (must use a pointer in main)
  2. malloc memory for the structure, and prompt/read in data about your structure
  3. Write 1 function that will print out that data

So... above is my code and i have a few questions:

  1. You may notice i have some getchar()'s in there, reason for that is because for whatever reason when i run ./a.out it always skips over the first scanf which i was guessing because when i press enter it gets stuck in stdin and resolves that to scanf, so putting the getchar()'s work.. but is there anyway to avoid this from happening all together? This never happened in any other of my programs...

  2. As you can see i am using fgets to get the string as the file name may contain spaces, which scanf doesn't work with. But my question is, do i have to store it in temp and then copy it over or could i just do: fgets(toUse->fileName, 100, stdin); I am assuming not, for the same reason i can't use this: toUse->fileName = "Test"; So is this correct the way i am currently doing it, or is there a better way?

  3. Now for the actual question that is making my program fail, the next reading in "Enter access time:", it will let me enter a number but as soon as i press enter i get a Seg Fault.. So why? Is it because i am using %lf? Is it something completely different? Should i just be using %l in the scanf? [When i do this, it will skip right over the question.. I am assuming for the same reason it skips over the other ones.. Something to deal with the enter]. So could this be it, the enter after i type the long "1234567890" is causing it to seg fault or am i doing something wrong?

Any questions answered would be helpful, thanks!

Foi útil?

Solução

  1. Try scanf("%c\n", ...) so that scanf waits for a return character

  2. fgets(toUse->fileName, 100, stdin); can work, toUse->fileName = "Test"; don't because "Test" is a pointer to the "Test" string, but toUse->fileName is a buffer, you can fgets in it directly

  3. %lf is a long float format. Try %ld in your scanf. Then you need to give scanf an address to write to, use scanf("%ld", &toUse->timestamp);

    Same for size: scanf("%d", toUse->size); should be scanf("%d", &toUse->size);

Outras dicas

It should be:

struct fileData *toUse = malloc(sizeof *toUse);
//                                     ^^^^^^

Alternatively:

struct fileData *toUse = malloc(sizeof(struct fileData));

You need to pass in the address of the variables:

scanf("%lf", &(toUse->timestamp));
printf("Enter the size:");
scanf("%d", &(toUse->size));

filename is an char array, so a reference to (filename,,,) is actually the address. The other values are scalars,not arrays.

Because you didn't allocate enough space.

SizeOf(toUse) = size of an address, on a 32 bit machine = 4 bytes, but clearly the size of your structure is more than 100 Bytes.

So, you need to allocate the sizeof the structure fileDate.

Change:

struct fileData *toUse = malloc(sizeof(toUse));

to:

struct fileData *toUse = malloc(sizeof(struct fileData));

and to avoid compiler warning for incompatible pointer types:

struct fileData *toUse = (struct fileData *) malloc(sizeof(struct fileData));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top