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.
1 messages · Page 1 of 1 (latest)
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.
That's not how you use fgets.
eh how then
i thought numbers is my puffer
max count to read is 100
and file is my file from which i read
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
That still doesn't address the issue.
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
You read into one string, not an array of them.
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
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
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.
this is the layout of my input file
i thought i want them all read in whhere a newline is a new string
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?
numbers is an array of chars.
what does it need to be
It stores the characters into the numbers array.
but they are float numbers
They are not floats.
The first character is 1, the next is .,...
So now to get a float you need to parse the string.
A string is an array of characters. A line is a string.
The string 1.323 consists of 5 characters: 1, ., 3, 2, 3.
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
@lime tendon Has your question been resolved? If so, type !solved :)
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?
so weird?
noone?