There are multiple ways to fix this. First, you can supply the complete path to the file in your code. Second, you can set the working directory for the Xcode scheme and set it to the directory where the file you want to open resides. In the project window toolbar to the right of the Run and Stop buttons is a path control. The left item in the path control is the name of your project. Click the item and choose Edit Scheme to open the scheme editor. Select the Run step from the left side of the scheme editor. Click the Options button at the top of the scheme editor. Select the Use custom working directory checkbox. Click the folder icon to open an Open panel to choose the working directory. I am having a similar issue as this post. However, on build I have 0 errors, 0 warning, and 0 messages. I took a look at Project -> Properties but I do not seem to have a linker option (which appears to be a typical fix that others have used to solve this issue). I have also tried Rebuilding Solution and then running but that does not seem to work either... Based on the aforementioned similar issue I understand the premise that "when an exe cannot be created (or in my case - found)... then naturally it cannot be run as it does not exist". However, I do not understand where the disconnect is occurring that is preventing this file from either being found or created... Any help sorting this out would be greatly appreciated. Cheers.
2 additional answers
Code: include <stdio.h>include <string.h>int main() { FILE ifp;char filename[20]; int i=0; int keyval[7]; int key[7]; int counter; printf("What is the name of the text file?\n"); gets(filename); ifp = fopen(filename, "r"); if (ifp == NULL){ printf("Error the file could not be opened.\n"); } else{ while(!feof(ifp)){ fscanf(ifp, "%d",keyval[i] ); printf("%d", keyval[i]); i++; } printf("In order to get my treasure you'll have to figure out which of my 100 keys are used in the 7 locks!\n"); printf("Which keys will you use?"); for(counter=0; counter < 7; counter++){ scanf("%d", key[counter]); } if(keyval[1]= key[1] && keyval[2]= key[2] && keyval[3]= key[3] && keyval[4]=key[4] && keyval[5]=key[5] && keyval[6]=key[6] && keyval[7]=key[7]){ printf("You've opened my treasure and found a map to the rest of the treasure on the island! Haha!\n"); } } fclose(ifp); return 0; } Now usually when the fopen() function returns NULL after trying to open a file for reading, the file is usually somewhere else. If your person on the computer types "keys.txt" the file needs to be in the same place as the program is. If it's in another directory, or on another drive, then it is part of the file path. File paths can get quite long much more than 20, typically for this reason.So you need to ask yourself: where is my file? is my string long enough to point to the file? -
Woah, let me see if I can help you by posting something people can actually copy and paste. Code: include <stdio.h>include <string.h>int main() { FILE ifp;char filename[20]; int i=0; int keyval[7]; int key[7]; int counter; printf("What is the name of the text file?\n"); gets(filename); ifp = fopen(filename, "r"); if (ifp == NULL){ printf("Error the file could not be opened.\n"); } else{ while(!feof(ifp)){ fscanf(ifp, "%d",keyval[i] ); printf("%d", keyval[i]); i++; } printf("In order to get my treasure you'll have to figure out which of my 100 keys are used in the 7 locks!\n"); printf("Which keys will you use?"); for(counter=0; counter < 7; counter++){ scanf("%d", key[counter]); } if(keyval[1]= key[1] && keyval[2]= key[2] && keyval[3]= key[3] && keyval[4]=key[4] && keyval[5]=key[5] && keyval[6]=key[6] && keyval[7]=key[7]){ printf("You've opened my treasure and found a map to the rest of the treasure on the island! Haha!\n"); } } fclose(ifp); return 0; } Now usually when the fopen() function returns NULL after trying to open a file for reading, the file is usually somewhere else. If your person on the computer types "keys.txt" the file needs to be in the same place as the program is. If it's in another directory, or on another drive, then it is part of the file path. File paths can get quite long much more than 20, typically for this reason.So you need to ask yourself: where is my file? is my string long enough to point to the file? So I tried making the string 100 long and putting the file where the project is located. Now when I run the program rather than getting the error message, it ask for the file from the user, and then I get the windows message that the program has stopped working and it is looking for a fix. Thanks for the reply -
Code: C:\Users\jk\Desktop>gcc -Wall treasure.c treasure.c: In function 'main': treasure.c:27:25: warning: format '%d' expects argument of type 'int ', but argument 3 has type 'int' [-Wformat=] fscanf(ifp, "%d",keyval[i] ); ^ treasure.c:36:19: warning: format '%d' expects argument of type 'int ', but argument 2 has type 'int' [-Wformat=]scanf("%d", key[counter]); treasure.c:39:144: error: lvalue required as left operand of assignment if(keyval[1]= key[1] && keyval[2]= key[2] && keyval[3]= key[3] && keyval[4]=key[4] && keyval[5]=key[5] && keyval[6]=key[6] && keyval[7]=key[7]){ Both of these can lead to a crash. You aren't calling scanf() correctly, and arrays start at 0. By attempting to read key[7] and keyval[7] to compare them, you overstep the bounds of the array. Also assignment (=) isn't the same as equality (==). One turns a variable's value into something else, the other sees if two things are the same or not. -
Yeah the program has lots more issues. Code: C:\Users\jk\Desktop>gcc -Wall treasure.c treasure.c: In function 'main': treasure.c:27:25: warning: format '%d' expects argument of type 'int ', but argument 3 has type 'int' [-Wformat=] fscanf(ifp, "%d",keyval[i] ); ^ treasure.c:36:19: warning: format '%d' expects argument of type 'int ', but argument 2 has type 'int' [-Wformat=]scanf("%d", key[counter]); treasure.c:39:144: error: lvalue required as left operand of assignment if(keyval[1]= key[1] && keyval[2]= key[2] && keyval[3]= key[3] && keyval[4]=key[4] && keyval[5]=key[5] && keyval[6]=key[6] && keyval[7]=key[7]){ Both of these can lead to a crash. You aren't calling scanf() correctly, and arrays start at 0. By attempting to read key[7] and keyval[7] to compare them, you overstep the bounds of the array. Also assignment (=) isn't the same as equality (==). One turns a variable's value into something else, the other sees if two things are the same or not. So I fixed the problem with the (=) sign and made the arrays go from 0-6 instead of 1-7. I'm not sure exactly how I'm using scanf wrong but when I run the program now I just get the file couldn't open message. -
Take another look at your lessons on scanf(). Scanf wants to write to a memory address. Your lines are missing key uses of the address-of operator. but when I run the program now I just get the file couldn't open message. Double check the name of the file that you want to open, and make sure that you get it right. Get the extension right, and everything. If it's not exactly the same, it won't find it to open. -
Take another look at your lessons on scanf(). Scanf wants to write to a memory address. Your lines are missing key uses of the address-of operator. Double check the name of the file that you want to open, and make sure that you get it right. Get the extension right, and everything. If it's not exactly the same, it won't find it to open. The only problem that I could think of is that %d is not what you use for integer arrays. I didn't think you used %s either so I don't know what to use there. -
These are the errors I am getting off your code in post. 3 Code: gcc -Wall -o "treasure" "treasure.c" (in directory: /home/userx/bin) treasure.c: In function 'main': treasure.c:18:5: warning: implicit declaration of function 'gets' [-Wimplicit-function-declaration] gets(filename);means no header to define function. Code: treasure.c:27:25: warning: format '%d' expects argument of type 'int ', but argument 3 has type 'int' [-Wformat=] fscanf(ifp, "%d",*keyval[i] );means it is expecting a int * (pointer) but is it being given just an int. the bold is the 3rd argument type int when it is looking for a int * Code: treasure.c:36:19: warning: format '%d' expects argument of type 'int ', but argument 2 has type 'int' [-Wformat=] scanf("%d", *key[counter]);same error only that it is with scanf and not fscanf it is complaining about your arrays not being shown using a pointer assignment. & or * Code: treasure.c:39:144: error: lvalue required as left operand of assignment if(keyval[1]= key[1] && keyval[2]= key[2] && keyval[3]= key[3] && keyval[4]=key[4] && keyval[5]=key[5] && keyval[6]=key[6] && keyval[7]=key[7]){ Compilation failed. means you're trying to assign the values to the left .. needs to be the comparison 'equals' not the assignment 'equals' Last edited by userxbw; 10-30-2017 at 07:21 AM. -
No, this probably means that you're using a modern compiler compiling for the current standard (C11) which has removed gets() from the standard. By the way using gets() with any compiler is a very very very bad idea, this function is unsafe and can never be used safely. Jim -
When you get the error: implicit declaration of function it should also list the offending function. Often this error happens because of a forgotten or missing header file, so at the shell prompt you can type man 2 function name it does not have the header added to the include statement so it knows what it is, prototype.[/quote] and that what you said too.... Last edited by userxbw; 10-30-2017 at 10:30 AM. -
This it what I was replying to. it does not have the header added to the include statement so it knows what it is, prototype. Until the C11 standard the <stdio.h> header file (which is included by the way) has the prototype for this dangerous function.The reason you are seeing this message is probably because you're compiling a C11 compatible program, unlike the OP. Since C11 removed the gets() function from the standard, it is no longer prototyped in <stdio.h> and the actual gets() function itself should no longer be part of the standard C library, hence your warning message. Jim -
11-11-2017 Registered User Your code has several problems. First of all, if you want to write in a file, use ofstream. ifstreamis only for reading files. |