fscanf to int and array
I am receiving a file as an argument in a C program
Each line has different numbers of integer like this :
1 2 4 5 6
7 8 8 9 10 -1 2 3
12 3 4 -2 2 -3 9 2 4
I want to get the first 2 arguments of each line into some int and all the rest into an array something like this for line #2 :
int a;
int b;
int c[10];
int a = 7;
int b = 8;
int c = [8,9,10,-1,1,2,3]
I am able to obtains the first 2 but I can't make it happens for the array.
Any help would be appreciated
This is what I have right now:
//get line per line until n
while (fscanf(fp, "%d %d", a, b) != EOF)
while (fscanf(fp,"%d [^n]", c[n]) != EOF)
n++;
// print each line
for ( int k = 0 ; k < 10; k ++)
printf("%d %d %d n", a, b, c[k]);
c arrays io scanf gets
add a comment |
I am receiving a file as an argument in a C program
Each line has different numbers of integer like this :
1 2 4 5 6
7 8 8 9 10 -1 2 3
12 3 4 -2 2 -3 9 2 4
I want to get the first 2 arguments of each line into some int and all the rest into an array something like this for line #2 :
int a;
int b;
int c[10];
int a = 7;
int b = 8;
int c = [8,9,10,-1,1,2,3]
I am able to obtains the first 2 but I can't make it happens for the array.
Any help would be appreciated
This is what I have right now:
//get line per line until n
while (fscanf(fp, "%d %d", a, b) != EOF)
while (fscanf(fp,"%d [^n]", c[n]) != EOF)
n++;
// print each line
for ( int k = 0 ; k < 10; k ++)
printf("%d %d %d n", a, b, c[k]);
c arrays io scanf gets
What have you tried ? Can you show us your code at least ?
– Theophile Dano
Nov 26 '18 at 15:38
1
What you had before the last edit was better, as it at least showed your attempt.
– dbush
Nov 26 '18 at 15:39
Just updated with new code that I'm trying right now
– dopeinc
Nov 26 '18 at 15:50
Should I use fgets and then use sscanf ? would be easier
– dopeinc
Nov 26 '18 at 15:52
add a comment |
I am receiving a file as an argument in a C program
Each line has different numbers of integer like this :
1 2 4 5 6
7 8 8 9 10 -1 2 3
12 3 4 -2 2 -3 9 2 4
I want to get the first 2 arguments of each line into some int and all the rest into an array something like this for line #2 :
int a;
int b;
int c[10];
int a = 7;
int b = 8;
int c = [8,9,10,-1,1,2,3]
I am able to obtains the first 2 but I can't make it happens for the array.
Any help would be appreciated
This is what I have right now:
//get line per line until n
while (fscanf(fp, "%d %d", a, b) != EOF)
while (fscanf(fp,"%d [^n]", c[n]) != EOF)
n++;
// print each line
for ( int k = 0 ; k < 10; k ++)
printf("%d %d %d n", a, b, c[k]);
c arrays io scanf gets
I am receiving a file as an argument in a C program
Each line has different numbers of integer like this :
1 2 4 5 6
7 8 8 9 10 -1 2 3
12 3 4 -2 2 -3 9 2 4
I want to get the first 2 arguments of each line into some int and all the rest into an array something like this for line #2 :
int a;
int b;
int c[10];
int a = 7;
int b = 8;
int c = [8,9,10,-1,1,2,3]
I am able to obtains the first 2 but I can't make it happens for the array.
Any help would be appreciated
This is what I have right now:
//get line per line until n
while (fscanf(fp, "%d %d", a, b) != EOF)
while (fscanf(fp,"%d [^n]", c[n]) != EOF)
n++;
// print each line
for ( int k = 0 ; k < 10; k ++)
printf("%d %d %d n", a, b, c[k]);
c arrays io scanf gets
c arrays io scanf gets
edited Nov 26 '18 at 16:16
gsamaras
51.4k24101188
51.4k24101188
asked Nov 25 '18 at 23:00
dopeincdopeinc
144
144
What have you tried ? Can you show us your code at least ?
– Theophile Dano
Nov 26 '18 at 15:38
1
What you had before the last edit was better, as it at least showed your attempt.
– dbush
Nov 26 '18 at 15:39
Just updated with new code that I'm trying right now
– dopeinc
Nov 26 '18 at 15:50
Should I use fgets and then use sscanf ? would be easier
– dopeinc
Nov 26 '18 at 15:52
add a comment |
What have you tried ? Can you show us your code at least ?
– Theophile Dano
Nov 26 '18 at 15:38
1
What you had before the last edit was better, as it at least showed your attempt.
– dbush
Nov 26 '18 at 15:39
Just updated with new code that I'm trying right now
– dopeinc
Nov 26 '18 at 15:50
Should I use fgets and then use sscanf ? would be easier
– dopeinc
Nov 26 '18 at 15:52
What have you tried ? Can you show us your code at least ?
– Theophile Dano
Nov 26 '18 at 15:38
What have you tried ? Can you show us your code at least ?
– Theophile Dano
Nov 26 '18 at 15:38
1
1
What you had before the last edit was better, as it at least showed your attempt.
– dbush
Nov 26 '18 at 15:39
What you had before the last edit was better, as it at least showed your attempt.
– dbush
Nov 26 '18 at 15:39
Just updated with new code that I'm trying right now
– dopeinc
Nov 26 '18 at 15:50
Just updated with new code that I'm trying right now
– dopeinc
Nov 26 '18 at 15:50
Should I use fgets and then use sscanf ? would be easier
– dopeinc
Nov 26 '18 at 15:52
Should I use fgets and then use sscanf ? would be easier
– dopeinc
Nov 26 '18 at 15:52
add a comment |
2 Answers
2
active
oldest
votes
Read C read file line by line and How to extract numbers from string in c?, and then you would end up with something like this:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void) {
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("input.txt", "r");
if (fp == NULL)
return -1;
while ((read = getline(&line, &len, fp)) != -1) {
printf("Retrieved line of length %zu:n", read);
//printf("%s", line);
char* p = line;
int a, b, c[10], counter = -2;
while (*p) { // While there are more characters to process...
if ( isdigit(*p) || ( (*p=='-'||*p=='+') && isdigit(*(p+1)) )) {
// Found a number
long val = strtol(p, &p, 10); // Read number
//printf("%ldn", val); // and print it.
if(counter == -2)
a = val;
else if(counter == -1)
b = val;
else if(counter < 10)
c[counter] = val;
counter++;
} else {
// Otherwise, move on to the next character.
p++;
}
}
printf("a = %d, b = %dn", a, b);
for(int i = 0; i < counter; ++i)
printf("%d ", c[i]);
printf("n");
}
fclose(fp);
if (line)
free(line);
return 0;
}
Output:
Retrieved line of length 10:
a = 1, b = 2
4 5 6
Retrieved line of length 18:
a = 7, b = 8
8 9 10 -1 2 3
Retrieved line of length 21:
a = 12, b = 3
4 -2 2 -3 9 2 4
add a comment |
Separating out lines using just fscanf can be tricky. You're better off reading a single line with fgets then using sscanf to read the numbers from there.
Also, fscanf and sscanf return the number of items matched. You shouldn't be checking for EOF.
char line[300];
while (fgets(line, sizeof(line), fp)) {
int rval = sscanf(line, "%d %d", &a, &b);
if (rval == 2) {
int count = 0;
do {
rval = sscanf(line, "%d", &c[count++]);
} while (rval == 1);
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472870%2ffscanf-to-int-and-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Read C read file line by line and How to extract numbers from string in c?, and then you would end up with something like this:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void) {
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("input.txt", "r");
if (fp == NULL)
return -1;
while ((read = getline(&line, &len, fp)) != -1) {
printf("Retrieved line of length %zu:n", read);
//printf("%s", line);
char* p = line;
int a, b, c[10], counter = -2;
while (*p) { // While there are more characters to process...
if ( isdigit(*p) || ( (*p=='-'||*p=='+') && isdigit(*(p+1)) )) {
// Found a number
long val = strtol(p, &p, 10); // Read number
//printf("%ldn", val); // and print it.
if(counter == -2)
a = val;
else if(counter == -1)
b = val;
else if(counter < 10)
c[counter] = val;
counter++;
} else {
// Otherwise, move on to the next character.
p++;
}
}
printf("a = %d, b = %dn", a, b);
for(int i = 0; i < counter; ++i)
printf("%d ", c[i]);
printf("n");
}
fclose(fp);
if (line)
free(line);
return 0;
}
Output:
Retrieved line of length 10:
a = 1, b = 2
4 5 6
Retrieved line of length 18:
a = 7, b = 8
8 9 10 -1 2 3
Retrieved line of length 21:
a = 12, b = 3
4 -2 2 -3 9 2 4
add a comment |
Read C read file line by line and How to extract numbers from string in c?, and then you would end up with something like this:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void) {
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("input.txt", "r");
if (fp == NULL)
return -1;
while ((read = getline(&line, &len, fp)) != -1) {
printf("Retrieved line of length %zu:n", read);
//printf("%s", line);
char* p = line;
int a, b, c[10], counter = -2;
while (*p) { // While there are more characters to process...
if ( isdigit(*p) || ( (*p=='-'||*p=='+') && isdigit(*(p+1)) )) {
// Found a number
long val = strtol(p, &p, 10); // Read number
//printf("%ldn", val); // and print it.
if(counter == -2)
a = val;
else if(counter == -1)
b = val;
else if(counter < 10)
c[counter] = val;
counter++;
} else {
// Otherwise, move on to the next character.
p++;
}
}
printf("a = %d, b = %dn", a, b);
for(int i = 0; i < counter; ++i)
printf("%d ", c[i]);
printf("n");
}
fclose(fp);
if (line)
free(line);
return 0;
}
Output:
Retrieved line of length 10:
a = 1, b = 2
4 5 6
Retrieved line of length 18:
a = 7, b = 8
8 9 10 -1 2 3
Retrieved line of length 21:
a = 12, b = 3
4 -2 2 -3 9 2 4
add a comment |
Read C read file line by line and How to extract numbers from string in c?, and then you would end up with something like this:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void) {
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("input.txt", "r");
if (fp == NULL)
return -1;
while ((read = getline(&line, &len, fp)) != -1) {
printf("Retrieved line of length %zu:n", read);
//printf("%s", line);
char* p = line;
int a, b, c[10], counter = -2;
while (*p) { // While there are more characters to process...
if ( isdigit(*p) || ( (*p=='-'||*p=='+') && isdigit(*(p+1)) )) {
// Found a number
long val = strtol(p, &p, 10); // Read number
//printf("%ldn", val); // and print it.
if(counter == -2)
a = val;
else if(counter == -1)
b = val;
else if(counter < 10)
c[counter] = val;
counter++;
} else {
// Otherwise, move on to the next character.
p++;
}
}
printf("a = %d, b = %dn", a, b);
for(int i = 0; i < counter; ++i)
printf("%d ", c[i]);
printf("n");
}
fclose(fp);
if (line)
free(line);
return 0;
}
Output:
Retrieved line of length 10:
a = 1, b = 2
4 5 6
Retrieved line of length 18:
a = 7, b = 8
8 9 10 -1 2 3
Retrieved line of length 21:
a = 12, b = 3
4 -2 2 -3 9 2 4
Read C read file line by line and How to extract numbers from string in c?, and then you would end up with something like this:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void) {
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("input.txt", "r");
if (fp == NULL)
return -1;
while ((read = getline(&line, &len, fp)) != -1) {
printf("Retrieved line of length %zu:n", read);
//printf("%s", line);
char* p = line;
int a, b, c[10], counter = -2;
while (*p) { // While there are more characters to process...
if ( isdigit(*p) || ( (*p=='-'||*p=='+') && isdigit(*(p+1)) )) {
// Found a number
long val = strtol(p, &p, 10); // Read number
//printf("%ldn", val); // and print it.
if(counter == -2)
a = val;
else if(counter == -1)
b = val;
else if(counter < 10)
c[counter] = val;
counter++;
} else {
// Otherwise, move on to the next character.
p++;
}
}
printf("a = %d, b = %dn", a, b);
for(int i = 0; i < counter; ++i)
printf("%d ", c[i]);
printf("n");
}
fclose(fp);
if (line)
free(line);
return 0;
}
Output:
Retrieved line of length 10:
a = 1, b = 2
4 5 6
Retrieved line of length 18:
a = 7, b = 8
8 9 10 -1 2 3
Retrieved line of length 21:
a = 12, b = 3
4 -2 2 -3 9 2 4
answered Nov 26 '18 at 16:00
gsamarasgsamaras
51.4k24101188
51.4k24101188
add a comment |
add a comment |
Separating out lines using just fscanf can be tricky. You're better off reading a single line with fgets then using sscanf to read the numbers from there.
Also, fscanf and sscanf return the number of items matched. You shouldn't be checking for EOF.
char line[300];
while (fgets(line, sizeof(line), fp)) {
int rval = sscanf(line, "%d %d", &a, &b);
if (rval == 2) {
int count = 0;
do {
rval = sscanf(line, "%d", &c[count++]);
} while (rval == 1);
}
}
add a comment |
Separating out lines using just fscanf can be tricky. You're better off reading a single line with fgets then using sscanf to read the numbers from there.
Also, fscanf and sscanf return the number of items matched. You shouldn't be checking for EOF.
char line[300];
while (fgets(line, sizeof(line), fp)) {
int rval = sscanf(line, "%d %d", &a, &b);
if (rval == 2) {
int count = 0;
do {
rval = sscanf(line, "%d", &c[count++]);
} while (rval == 1);
}
}
add a comment |
Separating out lines using just fscanf can be tricky. You're better off reading a single line with fgets then using sscanf to read the numbers from there.
Also, fscanf and sscanf return the number of items matched. You shouldn't be checking for EOF.
char line[300];
while (fgets(line, sizeof(line), fp)) {
int rval = sscanf(line, "%d %d", &a, &b);
if (rval == 2) {
int count = 0;
do {
rval = sscanf(line, "%d", &c[count++]);
} while (rval == 1);
}
}
Separating out lines using just fscanf can be tricky. You're better off reading a single line with fgets then using sscanf to read the numbers from there.
Also, fscanf and sscanf return the number of items matched. You shouldn't be checking for EOF.
char line[300];
while (fgets(line, sizeof(line), fp)) {
int rval = sscanf(line, "%d %d", &a, &b);
if (rval == 2) {
int count = 0;
do {
rval = sscanf(line, "%d", &c[count++]);
} while (rval == 1);
}
}
answered Nov 26 '18 at 15:58
dbushdbush
97.6k13104138
97.6k13104138
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472870%2ffscanf-to-int-and-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What have you tried ? Can you show us your code at least ?
– Theophile Dano
Nov 26 '18 at 15:38
1
What you had before the last edit was better, as it at least showed your attempt.
– dbush
Nov 26 '18 at 15:39
Just updated with new code that I'm trying right now
– dopeinc
Nov 26 '18 at 15:50
Should I use fgets and then use sscanf ? would be easier
– dopeinc
Nov 26 '18 at 15:52