How to fix Reading from a file?












-1















I have created a text file which contains:



CODE    Name       Atttack    Cost    Maintenance 
0 Villager 0 20 +15
1 Axeman 1 30 -5
2 Bowman 2 40 -5
3 Scout 1 10 -0
4 Troll 4 50 -10
5 Dragon 6 100 -30


Code:



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

int main() {
FILE * fptr; //Ρευμα
char ch;
fptr = fopen("UNITS.txt", "r");
if (fptr == NULL)
{
printf("Cannot open file n");
exit(0);
}
// Read contents from file
ch = fgetc(fptr);
while ((ch = fgetc(fptr)) != EOF){
printf("%c", ch);
}

fclose(fptr);
return 0;
}


I always get the message "cannot open file". Currently my file is on desktop, but I have tried to put the file in the directory where the executable file of the program is present.










share|improve this question




















  • 2





    Rather than printf("Cannot open file n");, try perror("UNITS.txt"). you'll get a more useful error message.

    – William Pursell
    Dec 29 '18 at 16:16











  • Do you have a case difference in the file name? "UNITS.txt" v. "units.txt"?

    – chux
    Dec 29 '18 at 16:20








  • 1





    When you open a file such as "UNITS.txt" your program will look for it in the current working directory. That may or may not be the one where the executable is located. If you are using an IDE such as VisualStudio, there is an option in the Debug project settings where you can choose the current working directory.

    – rodrigo
    Dec 29 '18 at 16:20











  • Try adding FILE * f; f = fopen("test.txt", "w"); fclose(f); and see where that file is created.

    – chux
    Dec 29 '18 at 16:21






  • 3





    Aside: Use int ch;, not char ch; as fgetc(fptr) returns 257 different values.

    – chux
    Dec 29 '18 at 16:22
















-1















I have created a text file which contains:



CODE    Name       Atttack    Cost    Maintenance 
0 Villager 0 20 +15
1 Axeman 1 30 -5
2 Bowman 2 40 -5
3 Scout 1 10 -0
4 Troll 4 50 -10
5 Dragon 6 100 -30


Code:



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

int main() {
FILE * fptr; //Ρευμα
char ch;
fptr = fopen("UNITS.txt", "r");
if (fptr == NULL)
{
printf("Cannot open file n");
exit(0);
}
// Read contents from file
ch = fgetc(fptr);
while ((ch = fgetc(fptr)) != EOF){
printf("%c", ch);
}

fclose(fptr);
return 0;
}


I always get the message "cannot open file". Currently my file is on desktop, but I have tried to put the file in the directory where the executable file of the program is present.










share|improve this question




















  • 2





    Rather than printf("Cannot open file n");, try perror("UNITS.txt"). you'll get a more useful error message.

    – William Pursell
    Dec 29 '18 at 16:16











  • Do you have a case difference in the file name? "UNITS.txt" v. "units.txt"?

    – chux
    Dec 29 '18 at 16:20








  • 1





    When you open a file such as "UNITS.txt" your program will look for it in the current working directory. That may or may not be the one where the executable is located. If you are using an IDE such as VisualStudio, there is an option in the Debug project settings where you can choose the current working directory.

    – rodrigo
    Dec 29 '18 at 16:20











  • Try adding FILE * f; f = fopen("test.txt", "w"); fclose(f); and see where that file is created.

    – chux
    Dec 29 '18 at 16:21






  • 3





    Aside: Use int ch;, not char ch; as fgetc(fptr) returns 257 different values.

    – chux
    Dec 29 '18 at 16:22














-1












-1








-1








I have created a text file which contains:



CODE    Name       Atttack    Cost    Maintenance 
0 Villager 0 20 +15
1 Axeman 1 30 -5
2 Bowman 2 40 -5
3 Scout 1 10 -0
4 Troll 4 50 -10
5 Dragon 6 100 -30


Code:



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

int main() {
FILE * fptr; //Ρευμα
char ch;
fptr = fopen("UNITS.txt", "r");
if (fptr == NULL)
{
printf("Cannot open file n");
exit(0);
}
// Read contents from file
ch = fgetc(fptr);
while ((ch = fgetc(fptr)) != EOF){
printf("%c", ch);
}

fclose(fptr);
return 0;
}


I always get the message "cannot open file". Currently my file is on desktop, but I have tried to put the file in the directory where the executable file of the program is present.










share|improve this question
















I have created a text file which contains:



CODE    Name       Atttack    Cost    Maintenance 
0 Villager 0 20 +15
1 Axeman 1 30 -5
2 Bowman 2 40 -5
3 Scout 1 10 -0
4 Troll 4 50 -10
5 Dragon 6 100 -30


Code:



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

int main() {
FILE * fptr; //Ρευμα
char ch;
fptr = fopen("UNITS.txt", "r");
if (fptr == NULL)
{
printf("Cannot open file n");
exit(0);
}
// Read contents from file
ch = fgetc(fptr);
while ((ch = fgetc(fptr)) != EOF){
printf("%c", ch);
}

fclose(fptr);
return 0;
}


I always get the message "cannot open file". Currently my file is on desktop, but I have tried to put the file in the directory where the executable file of the program is present.







c file-io






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 16:58









Brian Tompsett - 汤莱恩

4,2031337101




4,2031337101










asked Dec 29 '18 at 16:15









Rafail NicolaouRafail Nicolaou

1




1








  • 2





    Rather than printf("Cannot open file n");, try perror("UNITS.txt"). you'll get a more useful error message.

    – William Pursell
    Dec 29 '18 at 16:16











  • Do you have a case difference in the file name? "UNITS.txt" v. "units.txt"?

    – chux
    Dec 29 '18 at 16:20








  • 1





    When you open a file such as "UNITS.txt" your program will look for it in the current working directory. That may or may not be the one where the executable is located. If you are using an IDE such as VisualStudio, there is an option in the Debug project settings where you can choose the current working directory.

    – rodrigo
    Dec 29 '18 at 16:20











  • Try adding FILE * f; f = fopen("test.txt", "w"); fclose(f); and see where that file is created.

    – chux
    Dec 29 '18 at 16:21






  • 3





    Aside: Use int ch;, not char ch; as fgetc(fptr) returns 257 different values.

    – chux
    Dec 29 '18 at 16:22














  • 2





    Rather than printf("Cannot open file n");, try perror("UNITS.txt"). you'll get a more useful error message.

    – William Pursell
    Dec 29 '18 at 16:16











  • Do you have a case difference in the file name? "UNITS.txt" v. "units.txt"?

    – chux
    Dec 29 '18 at 16:20








  • 1





    When you open a file such as "UNITS.txt" your program will look for it in the current working directory. That may or may not be the one where the executable is located. If you are using an IDE such as VisualStudio, there is an option in the Debug project settings where you can choose the current working directory.

    – rodrigo
    Dec 29 '18 at 16:20











  • Try adding FILE * f; f = fopen("test.txt", "w"); fclose(f); and see where that file is created.

    – chux
    Dec 29 '18 at 16:21






  • 3





    Aside: Use int ch;, not char ch; as fgetc(fptr) returns 257 different values.

    – chux
    Dec 29 '18 at 16:22








2




2





Rather than printf("Cannot open file n");, try perror("UNITS.txt"). you'll get a more useful error message.

– William Pursell
Dec 29 '18 at 16:16





Rather than printf("Cannot open file n");, try perror("UNITS.txt"). you'll get a more useful error message.

– William Pursell
Dec 29 '18 at 16:16













Do you have a case difference in the file name? "UNITS.txt" v. "units.txt"?

– chux
Dec 29 '18 at 16:20







Do you have a case difference in the file name? "UNITS.txt" v. "units.txt"?

– chux
Dec 29 '18 at 16:20






1




1





When you open a file such as "UNITS.txt" your program will look for it in the current working directory. That may or may not be the one where the executable is located. If you are using an IDE such as VisualStudio, there is an option in the Debug project settings where you can choose the current working directory.

– rodrigo
Dec 29 '18 at 16:20





When you open a file such as "UNITS.txt" your program will look for it in the current working directory. That may or may not be the one where the executable is located. If you are using an IDE such as VisualStudio, there is an option in the Debug project settings where you can choose the current working directory.

– rodrigo
Dec 29 '18 at 16:20













Try adding FILE * f; f = fopen("test.txt", "w"); fclose(f); and see where that file is created.

– chux
Dec 29 '18 at 16:21





Try adding FILE * f; f = fopen("test.txt", "w"); fclose(f); and see where that file is created.

– chux
Dec 29 '18 at 16:21




3




3





Aside: Use int ch;, not char ch; as fgetc(fptr) returns 257 different values.

– chux
Dec 29 '18 at 16:22





Aside: Use int ch;, not char ch; as fgetc(fptr) returns 257 different values.

– chux
Dec 29 '18 at 16:22












1 Answer
1






active

oldest

votes


















0














Fixed i was using the wrong directory thanks.






share|improve this answer























    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%2f53971200%2fhow-to-fix-reading-from-a-file%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









    0














    Fixed i was using the wrong directory thanks.






    share|improve this answer




























      0














      Fixed i was using the wrong directory thanks.






      share|improve this answer


























        0












        0








        0







        Fixed i was using the wrong directory thanks.






        share|improve this answer













        Fixed i was using the wrong directory thanks.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 29 '18 at 19:27









        Rafail NicolaouRafail Nicolaou

        1




        1






























            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%2f53971200%2fhow-to-fix-reading-from-a-file%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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas