Reading comma separated numbers (coordinates) from a text file in CWhy is “while (!feof(file))” always wrong?How do I create a Java string from the contents of a file?Why should text files end with a newline?How do I save a String to a text file using Java?How to read/write from/to file using Go?How to read all files in a folder from Java?How to split a large text file into smaller files with equal number of lines?How to read a file line-by-line into a list?Easiest way to read from and write to filesHow to read a file in Groovy into a string?Read comma separated numbers from a file in C

Is it safe ? Is it scam or real?

Verb "geeitet" in an old scientific text

Does a card have a keyword if it has the same effect as said keyword?

What property of a BJT transistor makes it an amplifier?

What to use instead of cling film to wrap pastry

Did we get closer to another plane than we were supposed to, or was the pilot just protecting our delicate sensibilities?

Why isn't nylon as strong as kevlar?

Manager is threatening to grade me poorly if I don't complete the project

What is a smasher?

Can you complete the sequence?

How to display a value with zenity?

Point of the the Dothraki's attack in GoT S8E3?

What was the design of the Macintosh II's MMU replacement?

Are there any Final Fantasy Spirits in Super Smash Bros Ultimate?

Should I mention being denied entry to UK due to a confusion in my Visa and Ticket bookings?

How can I support myself financially as a 17 year old with a loan?

Expressing 'our' for objects belonging to our apartment

If I readied a spell with the trigger "When I take damage", do I have to make a constitution saving throw to avoid losing Concentration?

On which topic did Indiana Jones write his doctoral thesis?

How wide is a neg symbol, how to get the width for alignment?

How can modem speed be 10 times slower than router?

How to model the curly cable part of the phone

Can my company stop me from working overtime?

How important is people skills in academic career and applications?



Reading comma separated numbers (coordinates) from a text file in C


Why is “while (!feof(file))” always wrong?How do I create a Java string from the contents of a file?Why should text files end with a newline?How do I save a String to a text file using Java?How to read/write from/to file using Go?How to read all files in a folder from Java?How to split a large text file into smaller files with equal number of lines?How to read a file line-by-line into a list?Easiest way to read from and write to filesHow to read a file in Groovy into a string?Read comma separated numbers from a file in C






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'm trying to read some numbers in C from a text file which contains 1000 lines of numbers such as:



-340,495,-153,-910,835,-947 
-175,41,-421,-714,574,-645
-547,712,-352,579,951,-786
419,-864,-83,650,-399,171
...


but when I execute the code I see the i variable starts at 901 instead of 0 so it reads the numbers from the point i=901 to i=999. This is where I am experiencing problems.



I've tried to use a while loop instead of a for loop to avoid an extra i variable:



while(!feof(file_in))
fscanf(file_in,"%d%*c%d%*c",&a[0],&a[1]);
fscanf(file_in,"%d%*c%d%*c",&b[0],&b[1]);
fscanf(file_in,"%d%*c%d%*c",&c[0],&c[1]);
printf("A(%d,%d) I=%dn",a[0],a[1],i);
printf("B(%d,%d) I=%dn",b[0],b[1],i);
printf("C(%d,%d) I=%dn",c[0],c[1],i);



This is the part of the code I'm having hard time with:



int main(void) 
FILE *file_in;

int a[2];
int b[2];
int c[2];

file_in = fopen("triangles.txt","r");

if (file_in == NULL)
printf("Can't open file for reading.n");
exit(0);

else
for(int i=0;i<1000; i++)
fscanf(file_in,"%d%*c%d%*c",&a[0],&a[1]);
fscanf(file_in,"%d%*c%d%*c",&b[0],&b[1]);
fscanf(file_in,"%d%*c%dn",&c[0],&c[1]);
printf("A(%d,%d) I=%dn",a[0],a[1],i);
printf("B(%d,%d) I=%dn",b[0],b[1],i);
printf("C(%d,%d) I=%dn",c[0],c[1],i);

fclose(file_in);

return 0;



What I expect:



A(-340,495) I=0 
B(-153,-910) I=0
C(835,-947) I=0
A(-175,41) I=1
...


What I get:



B(-225,-818) I=901 
C(-808,-657) I=901
A(-279,-34) I=902
B(-533,-871) I=902
...









share|improve this question



















  • 1





    Note that while (!feof(file)) is always wrong.

    – Jonathan Leffler
    Mar 22 at 22:46











  • Note that if you fail to open the file, you attempt to call fclose() with a null pointer. This will not lead to happiness. Only close the file if you successfully opened it. It's often best to "fail quickly" — report the error (on stderr, not stdout) and exit, leaving the rest of the code one level less indented.

    – Jonathan Leffler
    Mar 22 at 22:49












  • What you've provided is fairly close to an MCVE (Minimal, Complete, and Verifiable example), but I think you should omit the call to consitOrigin() since you don't provide that code. If you find that the program works when you omit that call, then you're showing the wrong place. You should also check the return value from every call to fscanf() to ensure you get the values you expect. At the moment, you've no idea when things go wrong.

    – Jonathan Leffler
    Mar 22 at 22:54






  • 1





    Moving fclose() is the primary alternative; that works too. —— Are you testing the for loop code or the while loop code mainly? I don't see how i gets the wrong value with the for loop, but if you aren't really using i with the while loop, and in particular if you forget to initialize it, then you might see 901 instead of 0 as the first value. You still seem to be incrementing i, though.

    – Jonathan Leffler
    Mar 22 at 22:58







  • 1





    How are you examining the output of the program? If you are just dumping the output to a console, it may simply be that you have exceeded the console's scrollback. Check the console application's settings.

    – rici
    Mar 22 at 23:12

















0















I'm trying to read some numbers in C from a text file which contains 1000 lines of numbers such as:



-340,495,-153,-910,835,-947 
-175,41,-421,-714,574,-645
-547,712,-352,579,951,-786
419,-864,-83,650,-399,171
...


but when I execute the code I see the i variable starts at 901 instead of 0 so it reads the numbers from the point i=901 to i=999. This is where I am experiencing problems.



I've tried to use a while loop instead of a for loop to avoid an extra i variable:



while(!feof(file_in))
fscanf(file_in,"%d%*c%d%*c",&a[0],&a[1]);
fscanf(file_in,"%d%*c%d%*c",&b[0],&b[1]);
fscanf(file_in,"%d%*c%d%*c",&c[0],&c[1]);
printf("A(%d,%d) I=%dn",a[0],a[1],i);
printf("B(%d,%d) I=%dn",b[0],b[1],i);
printf("C(%d,%d) I=%dn",c[0],c[1],i);



This is the part of the code I'm having hard time with:



int main(void) 
FILE *file_in;

int a[2];
int b[2];
int c[2];

file_in = fopen("triangles.txt","r");

if (file_in == NULL)
printf("Can't open file for reading.n");
exit(0);

else
for(int i=0;i<1000; i++)
fscanf(file_in,"%d%*c%d%*c",&a[0],&a[1]);
fscanf(file_in,"%d%*c%d%*c",&b[0],&b[1]);
fscanf(file_in,"%d%*c%dn",&c[0],&c[1]);
printf("A(%d,%d) I=%dn",a[0],a[1],i);
printf("B(%d,%d) I=%dn",b[0],b[1],i);
printf("C(%d,%d) I=%dn",c[0],c[1],i);

fclose(file_in);

return 0;



What I expect:



A(-340,495) I=0 
B(-153,-910) I=0
C(835,-947) I=0
A(-175,41) I=1
...


What I get:



B(-225,-818) I=901 
C(-808,-657) I=901
A(-279,-34) I=902
B(-533,-871) I=902
...









share|improve this question



















  • 1





    Note that while (!feof(file)) is always wrong.

    – Jonathan Leffler
    Mar 22 at 22:46











  • Note that if you fail to open the file, you attempt to call fclose() with a null pointer. This will not lead to happiness. Only close the file if you successfully opened it. It's often best to "fail quickly" — report the error (on stderr, not stdout) and exit, leaving the rest of the code one level less indented.

    – Jonathan Leffler
    Mar 22 at 22:49












  • What you've provided is fairly close to an MCVE (Minimal, Complete, and Verifiable example), but I think you should omit the call to consitOrigin() since you don't provide that code. If you find that the program works when you omit that call, then you're showing the wrong place. You should also check the return value from every call to fscanf() to ensure you get the values you expect. At the moment, you've no idea when things go wrong.

    – Jonathan Leffler
    Mar 22 at 22:54






  • 1





    Moving fclose() is the primary alternative; that works too. —— Are you testing the for loop code or the while loop code mainly? I don't see how i gets the wrong value with the for loop, but if you aren't really using i with the while loop, and in particular if you forget to initialize it, then you might see 901 instead of 0 as the first value. You still seem to be incrementing i, though.

    – Jonathan Leffler
    Mar 22 at 22:58







  • 1





    How are you examining the output of the program? If you are just dumping the output to a console, it may simply be that you have exceeded the console's scrollback. Check the console application's settings.

    – rici
    Mar 22 at 23:12













0












0








0








I'm trying to read some numbers in C from a text file which contains 1000 lines of numbers such as:



-340,495,-153,-910,835,-947 
-175,41,-421,-714,574,-645
-547,712,-352,579,951,-786
419,-864,-83,650,-399,171
...


but when I execute the code I see the i variable starts at 901 instead of 0 so it reads the numbers from the point i=901 to i=999. This is where I am experiencing problems.



I've tried to use a while loop instead of a for loop to avoid an extra i variable:



while(!feof(file_in))
fscanf(file_in,"%d%*c%d%*c",&a[0],&a[1]);
fscanf(file_in,"%d%*c%d%*c",&b[0],&b[1]);
fscanf(file_in,"%d%*c%d%*c",&c[0],&c[1]);
printf("A(%d,%d) I=%dn",a[0],a[1],i);
printf("B(%d,%d) I=%dn",b[0],b[1],i);
printf("C(%d,%d) I=%dn",c[0],c[1],i);



This is the part of the code I'm having hard time with:



int main(void) 
FILE *file_in;

int a[2];
int b[2];
int c[2];

file_in = fopen("triangles.txt","r");

if (file_in == NULL)
printf("Can't open file for reading.n");
exit(0);

else
for(int i=0;i<1000; i++)
fscanf(file_in,"%d%*c%d%*c",&a[0],&a[1]);
fscanf(file_in,"%d%*c%d%*c",&b[0],&b[1]);
fscanf(file_in,"%d%*c%dn",&c[0],&c[1]);
printf("A(%d,%d) I=%dn",a[0],a[1],i);
printf("B(%d,%d) I=%dn",b[0],b[1],i);
printf("C(%d,%d) I=%dn",c[0],c[1],i);

fclose(file_in);

return 0;



What I expect:



A(-340,495) I=0 
B(-153,-910) I=0
C(835,-947) I=0
A(-175,41) I=1
...


What I get:



B(-225,-818) I=901 
C(-808,-657) I=901
A(-279,-34) I=902
B(-533,-871) I=902
...









share|improve this question
















I'm trying to read some numbers in C from a text file which contains 1000 lines of numbers such as:



-340,495,-153,-910,835,-947 
-175,41,-421,-714,574,-645
-547,712,-352,579,951,-786
419,-864,-83,650,-399,171
...


but when I execute the code I see the i variable starts at 901 instead of 0 so it reads the numbers from the point i=901 to i=999. This is where I am experiencing problems.



I've tried to use a while loop instead of a for loop to avoid an extra i variable:



while(!feof(file_in))
fscanf(file_in,"%d%*c%d%*c",&a[0],&a[1]);
fscanf(file_in,"%d%*c%d%*c",&b[0],&b[1]);
fscanf(file_in,"%d%*c%d%*c",&c[0],&c[1]);
printf("A(%d,%d) I=%dn",a[0],a[1],i);
printf("B(%d,%d) I=%dn",b[0],b[1],i);
printf("C(%d,%d) I=%dn",c[0],c[1],i);



This is the part of the code I'm having hard time with:



int main(void) 
FILE *file_in;

int a[2];
int b[2];
int c[2];

file_in = fopen("triangles.txt","r");

if (file_in == NULL)
printf("Can't open file for reading.n");
exit(0);

else
for(int i=0;i<1000; i++)
fscanf(file_in,"%d%*c%d%*c",&a[0],&a[1]);
fscanf(file_in,"%d%*c%d%*c",&b[0],&b[1]);
fscanf(file_in,"%d%*c%dn",&c[0],&c[1]);
printf("A(%d,%d) I=%dn",a[0],a[1],i);
printf("B(%d,%d) I=%dn",b[0],b[1],i);
printf("C(%d,%d) I=%dn",c[0],c[1],i);

fclose(file_in);

return 0;



What I expect:



A(-340,495) I=0 
B(-153,-910) I=0
C(835,-947) I=0
A(-175,41) I=1
...


What I get:



B(-225,-818) I=901 
C(-808,-657) I=901
A(-279,-34) I=902
B(-533,-871) I=902
...






c file






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 23:12







Tungdil

















asked Mar 22 at 22:35









TungdilTungdil

33




33







  • 1





    Note that while (!feof(file)) is always wrong.

    – Jonathan Leffler
    Mar 22 at 22:46











  • Note that if you fail to open the file, you attempt to call fclose() with a null pointer. This will not lead to happiness. Only close the file if you successfully opened it. It's often best to "fail quickly" — report the error (on stderr, not stdout) and exit, leaving the rest of the code one level less indented.

    – Jonathan Leffler
    Mar 22 at 22:49












  • What you've provided is fairly close to an MCVE (Minimal, Complete, and Verifiable example), but I think you should omit the call to consitOrigin() since you don't provide that code. If you find that the program works when you omit that call, then you're showing the wrong place. You should also check the return value from every call to fscanf() to ensure you get the values you expect. At the moment, you've no idea when things go wrong.

    – Jonathan Leffler
    Mar 22 at 22:54






  • 1





    Moving fclose() is the primary alternative; that works too. —— Are you testing the for loop code or the while loop code mainly? I don't see how i gets the wrong value with the for loop, but if you aren't really using i with the while loop, and in particular if you forget to initialize it, then you might see 901 instead of 0 as the first value. You still seem to be incrementing i, though.

    – Jonathan Leffler
    Mar 22 at 22:58







  • 1





    How are you examining the output of the program? If you are just dumping the output to a console, it may simply be that you have exceeded the console's scrollback. Check the console application's settings.

    – rici
    Mar 22 at 23:12












  • 1





    Note that while (!feof(file)) is always wrong.

    – Jonathan Leffler
    Mar 22 at 22:46











  • Note that if you fail to open the file, you attempt to call fclose() with a null pointer. This will not lead to happiness. Only close the file if you successfully opened it. It's often best to "fail quickly" — report the error (on stderr, not stdout) and exit, leaving the rest of the code one level less indented.

    – Jonathan Leffler
    Mar 22 at 22:49












  • What you've provided is fairly close to an MCVE (Minimal, Complete, and Verifiable example), but I think you should omit the call to consitOrigin() since you don't provide that code. If you find that the program works when you omit that call, then you're showing the wrong place. You should also check the return value from every call to fscanf() to ensure you get the values you expect. At the moment, you've no idea when things go wrong.

    – Jonathan Leffler
    Mar 22 at 22:54






  • 1





    Moving fclose() is the primary alternative; that works too. —— Are you testing the for loop code or the while loop code mainly? I don't see how i gets the wrong value with the for loop, but if you aren't really using i with the while loop, and in particular if you forget to initialize it, then you might see 901 instead of 0 as the first value. You still seem to be incrementing i, though.

    – Jonathan Leffler
    Mar 22 at 22:58







  • 1





    How are you examining the output of the program? If you are just dumping the output to a console, it may simply be that you have exceeded the console's scrollback. Check the console application's settings.

    – rici
    Mar 22 at 23:12







1




1





Note that while (!feof(file)) is always wrong.

– Jonathan Leffler
Mar 22 at 22:46





Note that while (!feof(file)) is always wrong.

– Jonathan Leffler
Mar 22 at 22:46













Note that if you fail to open the file, you attempt to call fclose() with a null pointer. This will not lead to happiness. Only close the file if you successfully opened it. It's often best to "fail quickly" — report the error (on stderr, not stdout) and exit, leaving the rest of the code one level less indented.

– Jonathan Leffler
Mar 22 at 22:49






Note that if you fail to open the file, you attempt to call fclose() with a null pointer. This will not lead to happiness. Only close the file if you successfully opened it. It's often best to "fail quickly" — report the error (on stderr, not stdout) and exit, leaving the rest of the code one level less indented.

– Jonathan Leffler
Mar 22 at 22:49














What you've provided is fairly close to an MCVE (Minimal, Complete, and Verifiable example), but I think you should omit the call to consitOrigin() since you don't provide that code. If you find that the program works when you omit that call, then you're showing the wrong place. You should also check the return value from every call to fscanf() to ensure you get the values you expect. At the moment, you've no idea when things go wrong.

– Jonathan Leffler
Mar 22 at 22:54





What you've provided is fairly close to an MCVE (Minimal, Complete, and Verifiable example), but I think you should omit the call to consitOrigin() since you don't provide that code. If you find that the program works when you omit that call, then you're showing the wrong place. You should also check the return value from every call to fscanf() to ensure you get the values you expect. At the moment, you've no idea when things go wrong.

– Jonathan Leffler
Mar 22 at 22:54




1




1





Moving fclose() is the primary alternative; that works too. —— Are you testing the for loop code or the while loop code mainly? I don't see how i gets the wrong value with the for loop, but if you aren't really using i with the while loop, and in particular if you forget to initialize it, then you might see 901 instead of 0 as the first value. You still seem to be incrementing i, though.

– Jonathan Leffler
Mar 22 at 22:58






Moving fclose() is the primary alternative; that works too. —— Are you testing the for loop code or the while loop code mainly? I don't see how i gets the wrong value with the for loop, but if you aren't really using i with the while loop, and in particular if you forget to initialize it, then you might see 901 instead of 0 as the first value. You still seem to be incrementing i, though.

– Jonathan Leffler
Mar 22 at 22:58





1




1





How are you examining the output of the program? If you are just dumping the output to a console, it may simply be that you have exceeded the console's scrollback. Check the console application's settings.

– rici
Mar 22 at 23:12





How are you examining the output of the program? If you are just dumping the output to a console, it may simply be that you have exceeded the console's scrollback. Check the console application's settings.

– rici
Mar 22 at 23:12












1 Answer
1






active

oldest

votes


















1














I don't think you've yet demonstrated the problem. This is a minor adaptation of your code, reporting errors to stderr, exiting on an error, and so on.



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

int main(void)

int a[2];
int b[2];
int c[2];
const char filename[] = "triangles.txt";
FILE *file_in = fopen(filename, "r");

if (file_in == NULL)

fprintf(stderr, "Can't open file %s for reading.n", filename);
exit(EXIT_FAILURE);


for (int i = 0; i < 1000; i++)

fclose(file_in);
return 0;



Note that it scrupulously checks the scanning operations, and reports problems if (when?) it encounters them.



Beware of the trailing newline in the format string. When the input is coming from a file, it is OK (not necessary, but OK), but it is a dire UI disaster if a human is ever expected to type the numbers.



Given the data file:



-340,495,-153,-910,835,-947 
-175,41,-421,-714,574,-645
-547,712,-352,579,951,-786
419,-864,-83,650,-399,171


I get the output:



A(-340, 495) I=0
B(-153,-910) I=0
C( 835,-947) I=0
A(-175, 41) I=1
B(-421,-714) I=1
C( 574,-645) I=1
A(-547, 712) I=2
B(-352, 579) I=2
C( 951,-786) I=2
A( 419,-864) I=3
B( -83, 650) I=3
C(-399, 171) I=3
Failed to read data for i = 4: r1 = -1, r2 = -1, r3 = -1


The last line appears on standard error, of course; the rest is written on standard output.



I think your problem is not in the code shown, therefore.






share|improve this answer























  • I guess the file I'm trying the read is so big that it overflows something because when I execute the code for a relatively small file everything works just fine. Here is the link to the file I'm trying to read link

    – Tungdil
    Mar 23 at 7:33











  • I tried an adaptation of the code above (flattening three lines of output per line of input into one line of output per line of input) on your big data file and it worked fine (except that three of the numbers were -1000 which threw the formatting off; %5d instead of %4d would have restored uniformity).

    – Jonathan Leffler
    Mar 23 at 21:17











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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55308647%2freading-comma-separated-numbers-coordinates-from-a-text-file-in-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I don't think you've yet demonstrated the problem. This is a minor adaptation of your code, reporting errors to stderr, exiting on an error, and so on.



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

int main(void)

int a[2];
int b[2];
int c[2];
const char filename[] = "triangles.txt";
FILE *file_in = fopen(filename, "r");

if (file_in == NULL)

fprintf(stderr, "Can't open file %s for reading.n", filename);
exit(EXIT_FAILURE);


for (int i = 0; i < 1000; i++)

fclose(file_in);
return 0;



Note that it scrupulously checks the scanning operations, and reports problems if (when?) it encounters them.



Beware of the trailing newline in the format string. When the input is coming from a file, it is OK (not necessary, but OK), but it is a dire UI disaster if a human is ever expected to type the numbers.



Given the data file:



-340,495,-153,-910,835,-947 
-175,41,-421,-714,574,-645
-547,712,-352,579,951,-786
419,-864,-83,650,-399,171


I get the output:



A(-340, 495) I=0
B(-153,-910) I=0
C( 835,-947) I=0
A(-175, 41) I=1
B(-421,-714) I=1
C( 574,-645) I=1
A(-547, 712) I=2
B(-352, 579) I=2
C( 951,-786) I=2
A( 419,-864) I=3
B( -83, 650) I=3
C(-399, 171) I=3
Failed to read data for i = 4: r1 = -1, r2 = -1, r3 = -1


The last line appears on standard error, of course; the rest is written on standard output.



I think your problem is not in the code shown, therefore.






share|improve this answer























  • I guess the file I'm trying the read is so big that it overflows something because when I execute the code for a relatively small file everything works just fine. Here is the link to the file I'm trying to read link

    – Tungdil
    Mar 23 at 7:33











  • I tried an adaptation of the code above (flattening three lines of output per line of input into one line of output per line of input) on your big data file and it worked fine (except that three of the numbers were -1000 which threw the formatting off; %5d instead of %4d would have restored uniformity).

    – Jonathan Leffler
    Mar 23 at 21:17















1














I don't think you've yet demonstrated the problem. This is a minor adaptation of your code, reporting errors to stderr, exiting on an error, and so on.



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

int main(void)

int a[2];
int b[2];
int c[2];
const char filename[] = "triangles.txt";
FILE *file_in = fopen(filename, "r");

if (file_in == NULL)

fprintf(stderr, "Can't open file %s for reading.n", filename);
exit(EXIT_FAILURE);


for (int i = 0; i < 1000; i++)

fclose(file_in);
return 0;



Note that it scrupulously checks the scanning operations, and reports problems if (when?) it encounters them.



Beware of the trailing newline in the format string. When the input is coming from a file, it is OK (not necessary, but OK), but it is a dire UI disaster if a human is ever expected to type the numbers.



Given the data file:



-340,495,-153,-910,835,-947 
-175,41,-421,-714,574,-645
-547,712,-352,579,951,-786
419,-864,-83,650,-399,171


I get the output:



A(-340, 495) I=0
B(-153,-910) I=0
C( 835,-947) I=0
A(-175, 41) I=1
B(-421,-714) I=1
C( 574,-645) I=1
A(-547, 712) I=2
B(-352, 579) I=2
C( 951,-786) I=2
A( 419,-864) I=3
B( -83, 650) I=3
C(-399, 171) I=3
Failed to read data for i = 4: r1 = -1, r2 = -1, r3 = -1


The last line appears on standard error, of course; the rest is written on standard output.



I think your problem is not in the code shown, therefore.






share|improve this answer























  • I guess the file I'm trying the read is so big that it overflows something because when I execute the code for a relatively small file everything works just fine. Here is the link to the file I'm trying to read link

    – Tungdil
    Mar 23 at 7:33











  • I tried an adaptation of the code above (flattening three lines of output per line of input into one line of output per line of input) on your big data file and it worked fine (except that three of the numbers were -1000 which threw the formatting off; %5d instead of %4d would have restored uniformity).

    – Jonathan Leffler
    Mar 23 at 21:17













1












1








1







I don't think you've yet demonstrated the problem. This is a minor adaptation of your code, reporting errors to stderr, exiting on an error, and so on.



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

int main(void)

int a[2];
int b[2];
int c[2];
const char filename[] = "triangles.txt";
FILE *file_in = fopen(filename, "r");

if (file_in == NULL)

fprintf(stderr, "Can't open file %s for reading.n", filename);
exit(EXIT_FAILURE);


for (int i = 0; i < 1000; i++)

fclose(file_in);
return 0;



Note that it scrupulously checks the scanning operations, and reports problems if (when?) it encounters them.



Beware of the trailing newline in the format string. When the input is coming from a file, it is OK (not necessary, but OK), but it is a dire UI disaster if a human is ever expected to type the numbers.



Given the data file:



-340,495,-153,-910,835,-947 
-175,41,-421,-714,574,-645
-547,712,-352,579,951,-786
419,-864,-83,650,-399,171


I get the output:



A(-340, 495) I=0
B(-153,-910) I=0
C( 835,-947) I=0
A(-175, 41) I=1
B(-421,-714) I=1
C( 574,-645) I=1
A(-547, 712) I=2
B(-352, 579) I=2
C( 951,-786) I=2
A( 419,-864) I=3
B( -83, 650) I=3
C(-399, 171) I=3
Failed to read data for i = 4: r1 = -1, r2 = -1, r3 = -1


The last line appears on standard error, of course; the rest is written on standard output.



I think your problem is not in the code shown, therefore.






share|improve this answer













I don't think you've yet demonstrated the problem. This is a minor adaptation of your code, reporting errors to stderr, exiting on an error, and so on.



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

int main(void)

int a[2];
int b[2];
int c[2];
const char filename[] = "triangles.txt";
FILE *file_in = fopen(filename, "r");

if (file_in == NULL)

fprintf(stderr, "Can't open file %s for reading.n", filename);
exit(EXIT_FAILURE);


for (int i = 0; i < 1000; i++)

fclose(file_in);
return 0;



Note that it scrupulously checks the scanning operations, and reports problems if (when?) it encounters them.



Beware of the trailing newline in the format string. When the input is coming from a file, it is OK (not necessary, but OK), but it is a dire UI disaster if a human is ever expected to type the numbers.



Given the data file:



-340,495,-153,-910,835,-947 
-175,41,-421,-714,574,-645
-547,712,-352,579,951,-786
419,-864,-83,650,-399,171


I get the output:



A(-340, 495) I=0
B(-153,-910) I=0
C( 835,-947) I=0
A(-175, 41) I=1
B(-421,-714) I=1
C( 574,-645) I=1
A(-547, 712) I=2
B(-352, 579) I=2
C( 951,-786) I=2
A( 419,-864) I=3
B( -83, 650) I=3
C(-399, 171) I=3
Failed to read data for i = 4: r1 = -1, r2 = -1, r3 = -1


The last line appears on standard error, of course; the rest is written on standard output.



I think your problem is not in the code shown, therefore.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 22 at 23:11









Jonathan LefflerJonathan Leffler

577k966911046




577k966911046












  • I guess the file I'm trying the read is so big that it overflows something because when I execute the code for a relatively small file everything works just fine. Here is the link to the file I'm trying to read link

    – Tungdil
    Mar 23 at 7:33











  • I tried an adaptation of the code above (flattening three lines of output per line of input into one line of output per line of input) on your big data file and it worked fine (except that three of the numbers were -1000 which threw the formatting off; %5d instead of %4d would have restored uniformity).

    – Jonathan Leffler
    Mar 23 at 21:17

















  • I guess the file I'm trying the read is so big that it overflows something because when I execute the code for a relatively small file everything works just fine. Here is the link to the file I'm trying to read link

    – Tungdil
    Mar 23 at 7:33











  • I tried an adaptation of the code above (flattening three lines of output per line of input into one line of output per line of input) on your big data file and it worked fine (except that three of the numbers were -1000 which threw the formatting off; %5d instead of %4d would have restored uniformity).

    – Jonathan Leffler
    Mar 23 at 21:17
















I guess the file I'm trying the read is so big that it overflows something because when I execute the code for a relatively small file everything works just fine. Here is the link to the file I'm trying to read link

– Tungdil
Mar 23 at 7:33





I guess the file I'm trying the read is so big that it overflows something because when I execute the code for a relatively small file everything works just fine. Here is the link to the file I'm trying to read link

– Tungdil
Mar 23 at 7:33













I tried an adaptation of the code above (flattening three lines of output per line of input into one line of output per line of input) on your big data file and it worked fine (except that three of the numbers were -1000 which threw the formatting off; %5d instead of %4d would have restored uniformity).

– Jonathan Leffler
Mar 23 at 21:17





I tried an adaptation of the code above (flattening three lines of output per line of input into one line of output per line of input) on your big data file and it worked fine (except that three of the numbers were -1000 which threw the formatting off; %5d instead of %4d would have restored uniformity).

– Jonathan Leffler
Mar 23 at 21:17



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55308647%2freading-comma-separated-numbers-coordinates-from-a-text-file-in-c%23new-answer', 'question_page');

);

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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현