Lỗi unable open input file cpp trong c năm 2024

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...

Lỗi unable open input file cpp trong c năm 2024

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.

Lỗi unable open input file cpp trong c năm 2024

  1. Lỗi unable open input file cpp trong c năm 2024
    Viorel 109.2K Reputation points 2021-07-18T05:36:01.533+00:00 It seems that you created an empty project and main.cpp is not included into project. Try creating a new “Console App” project, check that it runs, then add your code. It is possible to add existing files too.

2 additional answers

  1. 2021-07-18T17:15:34.34+00:00 When I created the project I went to File -> New -> File instead of the way @Sam of Simple Samples suggested. Here is some documentation on how to do it correctly (both for myself and for anyone else who comes across this in the future). Thank you @Viorel for pointing out my error. I am getting it sorted out now. Cheers!
  2. 2022-11-12T20:55:23.707+00:00

    I have a similar problem. My builds/rebuilds have 0 errors/0 warnings. The code has been built correctly, or errors fixed, and built for 5+ months. My main file has a main in it, unchanged for 5+ months. My Debug directory has an .exe in it. My object file directory, <program name>/Debug has .obj files in it but no .exe file. I have no external libraries that I link to. I have done a clean and build/rebuild. I have looked at previous posts. I have checked my Tools->Options and I seem to have all of the posted options set. Your friend has recently come into possession of an old treasure chest. They are convinced there is great treasure inside. They may not even need to go to the Caribbean to get treasure! The treasure chest is locked with seven locks and your friend has found 100 keys that might fit these locks. Your friend and their crew members will have to try different combinations of keys to try and get the treasure chest open. There�s one other thing. The treasure chest is haunted! The pirate who previously owned this treasure is still around, reminding other pirates that each lock has a unique key and letting them know if any of their current guesses are correct. Your program will need to read in a file of the correct combination of keys, numbered 1-100. Then, prompt the user for the seven numbers indicating the seven keys they wish to use. They cannot use the same key twice in one attempt. If they have all the correct keys in the exactly correct order, they can open the chest. If they have some correct keys, regardless of order, let the user know how many keys are correct. Input File Format The input file will contain 7 unique integers from 0 to 100. Program Specification You must use arrays to solve the problem. Your program should first prompt the user for the name of the input file. Then, your program should process the input file and copy the correct order of keys into the program. Then you can prompt the user for their first guess. If the user attempts to use a key more than once in a single guess, tell them they can only use each key once. If the user exactly matches the keys and the order, let them open the chest. If the user identifies some of the correct keys, let them know how many keys are correct but tell them they may not be in the right order. And here is the code I currently have: Code:

    11
    -------
    printf("What is the name of the text file?\n");
    13
    ---
    ifp = fopen(filename, "r");
    16
    --------
    printf("Error the file could not be opened.\n");
    20
    -----
    fscanf(ifp, "%d",keyval[i] );
    21
    ----
    printf("%d", keyval[i]);
    24
    ------------
    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");
    25
    ---
    printf("Which keys will you use?");
    26
    -------
    for(counter=0; counter < 7; counter++){
    27
    --
    scanf("%d", key[counter]);
    29
    ----------------
    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]){
    30
    -----------
    printf("You've opened my treasure and found a map to the rest of the treasure on the island! Haha!\n");
    So for some reason when I try to open the file it is always NULL, and just prints the error message that I put in. The name of the file is keylist.txt. I could also use some advice on how to go about checking if any keys are matched and how to print out the number of matches. I'm also not sure how to check to see if they use the same key more than once. Any help would be appreciated, thanks!
  3. 10-29-2017
    Lỗi unable open input file cpp trong c năm 2024
    Lurking
    Lỗi unable open input file cpp trong c năm 2024
    ---
    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?

-
  1. 10-29-2017

    Lỗi unable open input file cpp trong c năm 2024
    Registered User

    -
    Lỗi unable open input file cpp trong c năm 2024
    Originally Posted by whiteflags

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

-
  1. 10-29-2017

    Lỗi unable open input file cpp trong c năm 2024
    Lurking
    Lỗi unable open input file cpp trong c năm 2024

    - 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.

-
  1. 10-29-2017

    Lỗi unable open input file cpp trong c năm 2024
    Registered User

    -
    Lỗi unable open input file cpp trong c năm 2024
    Originally Posted by whiteflags

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.

-
  1. 10-29-2017

    Lỗi unable open input file cpp trong c năm 2024
    Lurking
    Lỗi unable open input file cpp trong c năm 2024

    - I'm not sure exactly how I'm using scanf wrong

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.

-
  1. 10-29-2017

    Lỗi unable open input file cpp trong c năm 2024
    Registered User

    -
    Lỗi unable open input file cpp trong c năm 2024
    Originally Posted by whiteflags

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.

-
  1. 10-30-2017

    Lỗi unable open input file cpp trong c năm 2024
    Banned

    - it is not the %d part that whiteflags is refereeing to, it is the how to get the information into or read from the information within the type of storage container yo are using, ( and the names one uses to describe items in programming). the container being an array and pointers.

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.

-
  1. 10-30-2017

    Lỗi unable open input file cpp trong c năm 2024
    Registered User

    - means no header to define function.

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

-
  1. 10-30-2017

    Lỗi unable open input file cpp trong c năm 2024
    Banned

    - the error implicit declaration

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.

-
  1. 10-30-2017

    Lỗi unable open input file cpp trong c năm 2024
    Registered User

    - These are the errors I am getting off your code in post. 3

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

-
  1. 10-30-2017

    Lỗi unable open input file cpp trong c năm 2024
    Banned

    - I was more focused on reading error messages and knowing what they are telling someone so they can trouble shoot their code, but yes you too are correct I am not arguing that point, only now stating it is about time they took that function out. Because I personally am tired of seeing everyone that sees someone using gets being told do not use that function when they should have removed it a long time ago. so all we are seeing here is Murphy's law being applied. have a nice day. ---

11-11-2017

Lỗi unable open input file cpp trong c năm 2024

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.