Read text file in xcode

I tried to read the text file in xcode, but this "EXC_BAD_ACCESS message appeared when I tried to create my program

here is my code and I paste the inputA.txt file into the same folder with the project file my friend told me that I have to put the txt file in the folder for debugging, so I can not read the txt file in this code? please help me...

macbook user.

int main (int argc, const char * argv[]) {
    FILE* fp;  
    char mychar;  
    char arr[50][2] = {0, };  
    int i = 0;  
    int j, k;  
    graphType* G_;  
    G_ = (graphType*)malloc(sizeof(graphType));  
    Create(G_); 
    fp = fopen("inputA.txt", "r");
    //fp = fopen("inputB.txt", "r");
    //fp = fopen("inputC.txt", "r");

    while(1){
        for(j = 0 ; j < 2 ; j++){
            mychar = fgetc(fp);
            if(mychar == EOF)
                break;
            else if(mychar == ' ')
                continue;
            arr[i][j] = mychar;
        }
        i++;
    }
+3
source share
3 answers

By default, your binary will be generated in ProjectDir/build/Mode, and Modewill be Debugor Release, and will have it as a working directory. If you want to access the file in the project directory, you will have to use it ../../input.txtin this case.

" " "" "". ( "", " :" ), .

, , :

#include <unistd.h>
int main() {
    char buf[2048];
    getcwd(buf, sizeof(buf));
    printf("%s", buf);
}
+2

, inputA.txt , . , ( ).

, fopen NULL, , .

if (fp == NULL)
{
    printf("Could not open file!");
    return 1;
}
0

fopen, , null, . !

0

Source: https://habr.com/ru/post/1746751/


All Articles