#Reading from a file

1 messages · Page 1 of 1 (latest)

warped galeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

gray solar
#

That's not how you use fgets.

lime tendon
#

eh how then

#

i thought numbers is my puffer

#

max count to read is 100

#

and file is my file from which i read

gray solar
#

fgets reads a string.

#

numbers is not a string.

lime tendon
#
int main(int argc, char *argv[]) {
    char dateiname[255];
    float numbers[1000];

    if (argv[1] == NULL) {
        printf("Bitte geben sie einen Dateinamen ein\n");
        scanf("%s", dateiname);
    }

    FILE *file = fopen(dateiname, "r");

    while (fgets(numbers, 10 , file)) {
        fputs(dateiname, stdout);
    }

    for (int i = 0; i < 30; i++) {
        printf("%f\n", numbers[i]);
    }

    return  0;
}
#

have it like this now

gray solar
#

That still doesn't address the issue.

lime tendon
#
int main(int argc, char *argv[]) {
    char dateiname[255];
    char *numbers[1000];

    if (argv[1] == NULL) {
        printf("Bitte geben sie einen Dateinamen ein\n");
        scanf("%s", dateiname);
    }

    FILE *file = fopen(dateiname, "r");

    while (fgets(numbers, 10 , file)) {
        fputs(dateiname, stdout);
    }

    for (int i = 0; i < 30; i++) {
        printf("%s\n", numbers[i]);
    }

    return  0;
}
#

so a array of a string

#

this is my output

gray solar
#

You read into one string, not an array of them.

lime tendon
#
int main(int argc, char *argv[]) {
    char dateiname[255];
    char *numbers[1000];

    if (argv[1] == NULL) {
        printf("Bitte geben sie einen Dateinamen ein\n");
        scanf("%s", dateiname);
    }

    FILE *file = fopen(dateiname, "r");

    while (fgets(*numbers, 10 , file)) {
        fputs(dateiname, stdout);
    }

    for (int i = 0; i < 30; i++) {
        printf("%s\n", numbers[i]);
    }

    return  0;
}
#

like this

#

output still the same

gray solar
#

The array is not initialized.

#

Honestly you need to read about how to use strings.

lime tendon
#
 char *numbers[1000] = {};
#

do you have any recommendations

#

quite new but we need to know all that stuff

#

we only have 2 weeks for a fkn c exam

lime tendon
#

thanks can you maybe help me solve this issue?

#

i initialized it

gray solar
#

You initialized it but with unusable values.

#

You read into one string, not an array of them.
This is key. You don't need an array of strings.

lime tendon
#

this is the layout of my input file

#

i thought i want them all read in whhere a newline is a new string

gray solar
#

The string is only used once.

#

With this kind of file though fscanf suffices.

lime tendon
#
int main(int argc, char *argv[]) {
    char dateiname[255];
    char numbers[1000] = {};

    if (argv[1] == NULL) {
        printf("Bitte geben sie einen Dateinamen ein\n");
        scanf("%s", dateiname);
    }

    FILE *file = fopen(dateiname, "r");

    while (fgets(numbers, 10 , file)) {
        fputs(dateiname, stdout);
    }

    for (int i = 0; i < 30; i++) {
        printf("%c\n", numbers[i]);
    }

    return  0;
}

now i dont have an array of chars anymore

#

what will fgets write into the buffer?

gray solar
#

numbers is an array of chars.

lime tendon
#

what does it need to be

gray solar
#

Like that.

#

You got it correct.

lime tendon
#

jeah so where does it read the input from the file

#

does he safe it in this array?

gray solar
#

It stores the characters into the numbers array.

lime tendon
#

but they are float numbers

gray solar
#

They are not floats.

lime tendon
#

e.g. one line is 1.323

#

how can he safe 1.323 in a char?

gray solar
#

The first character is 1, the next is .,...

lime tendon
#

even if it gets written as char and not fload

#

jeah

gray solar
#

So now to get a float you need to parse the string.

lime tendon
#

isnt it possible to read in a whole line?

#

and not every single char

gray solar
#

You are already reading in a whole line.

#

A line consists of characters.

lime tendon
#

ah

#

so for every loop

#

he reads in

#

1 line as single characters

gray solar
#

A string is an array of characters. A line is a string.

#

The string 1.323 consists of 5 characters: 1, ., 3, 2, 3.

lime tendon
#

jeah i know that

#
 char numbers[1000] = {};

    if (argv[1] == NULL) {
        printf("Bitte geben sie einen Dateinamen ein\n");
        scanf("%s", dateiname);
    }

    FILE *file = fopen(dateiname, "r");

    while (fgets(numbers, 10 , file)) {
        fputs(numbers, file);
    }

#

fgets takes the buffer, max, and a file to get the characters from

warped galeBOT
#

@lime tendon Has your question been resolved? If so, type !solved :)

lime tendon
#

got it kinda working now

#
int main(int argc, char *argv[]) {
    char dateiname[255];
    char numbers[1000] = {};
    FILE *file;
    if (argv[1] == NULL) {
        printf("Bitte geben sie einen Dateinamen ein\n");
        scanf("%s", dateiname);
    }
    
    file = fopen(dateiname, "r");
    while (fgets(numbers, 10 , file) != NULL) {
        puts(numbers);

    }

    for (int i = 0; i < 10; i++) {
        printf("%c", numbers[i]);
    }

    fclose(file);

    return  0;
}
#

output is like this

#

but im asking myself how is the last for loop working

#

it should only print a "c" but it kinda prints a whole string?

lime tendon
#

so weird?

lime tendon
#

noone?