Cannot open source file in Visual Studio 2022 - visual-c++

Currently, I am trying to make a simple out put and input game using <iostream> but whenever I type #include <iostream> an error pops up saying "cannot open source file "iostream."" I have tried to copy the file path, but personally, I have no idea how to do that, even with tutorials.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome human!\n" << endl;
return 0;
}
If you respond on how to do the file directory, do explain like you are trying to explain it to a c++ beginner since I recently started. :)

Related

Why is it an error to define a type before the stdafx.h header in Microsoft Visual C++? [duplicate]

This question already has answers here:
skipped when looking for precompiled header
(11 answers)
Closed 8 years ago.
As a simple example:
struct Month
{
int day;
};
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
Month a;
a.day = 5;
return 0;
}
Attempting to compile this in VS2008, VS2010 or VS2012 results in a C2065 (undeclared identifier error). Why?
Microsoft Visual C++ will not compile anything prior to the #include "stdafx.h" in the source file; it assumes all code in the source up to and including that line is already compiled.
The wikipedia page for precompiled headers states this and it's confirmed on Microsoft's own pages:
The compiler treats all code occurring before the .h file as precompiled. It skips to just beyond the #include directive associated with the .h file, uses the code contained in the .pch file, and then compiles all code after filename.
What's probably happening under the covers is that the creation of a pre-compiled header file (.pch) involves the storage of state at that point. Then, when you include the header, it just loads up that state as-is (overwriting the current state) and everything that has been compiled in the source file before that #include line is effectively thrown away.

VC++ play sound

I need C++ code to play sound at particular decibels that are choose by user dynamically.
This was answered Here
You need to use the absolute path, make sure that you're sending a filename (use SND_FILENAME flag), and pause the program long enough to play the sound file (e.g., use getchar()). You need to link the winmm.lib library in your project settings, and #include windows.h and mmsystem.h in the header.
#include <windows.h>
#include <mmsystem.h>
int main() {
PlaySound((LPCSTR) "C:\\kenny g.WAV", NULL, SND_FILENAME | SND_ASYNC);
getchar();
}

Inquiry: Integrating libsndfile with Visual Studio 2010 C++. Error: libsndfile.dll not found

I am teaching myself how to read in wav files into C++ as a part of me learning C++. I have found many resources online that recommended the following library: libsnfile library
So I followed some tutorials below in testing the basic functionality of the library, but I can't get the library to compile with Visual Studio 2010.
I have searched online for the following error, but did not find anything useful for my particular error. I downloaded the libsndfile C++ windows installer found here. I used the 32bit version since I am using the win32 C++ console version. However, my Visual Studio is 64 bit. I did the following after I downloaded the installer:
I went into Visual Studio. Under my project, I did the following:
In project properties:
1. VC++
Include >> added ...\libsnfile\include
Library >> added ...\libsnfile\lib
2. C\C++
Added the following directory as additional dependencies
...\libsnfile\lib\libsndfile-1.lib
I did this to add this third party library to my project. After this, to test, I ran the following code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>
int _tmain(int argc, _TCHAR* argv[])
{
printf("This is a test\n");
getchar();
return 0;
}
I coded that to make sure that I could access the sndfile.h in my program and everything compiled. The problem occured when I tried to implement the following code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>
int _tmain(int argc, _TCHAR* argv[])
{
printf("This is a test\n");
//This will be the length of the buffer used to hold samples while the program processes them.
//A SNDFILE is like FILE in a standard C library. Consequently, the sf_open_read and sf_open_write functions will return an
//SNDFILE* pointer when they successfully open the specified file.
SNDFILE* sf = NULL;
/*SF_INFO will obtain information of the file we wish to load into our program. */
SF_INFO info;
/*This is where the program will open the WAV file */
info.format = 0;
sf = sf_open("C:\Users\GeekyOmega\Desktop\gameon.wav", SFM_READ, &info);
if(sf == NULL)
{
printf("Failed to open the file.\n");
exit(-1);
}
getchar();
return 0;
}
I then get a system error when I click run inside visual studio when I try to run my program. It says,
The program can't start because libsnfile-1.dll is missing from your computer.
Try reinstalling the program to fix this problem.`
I tried the 64 bit windows installer and tried that, but it didn't work. Anyone understand what I am doing run? I am running Visual Studio's 2010 on Windows 7 as my dev environment.
I apologize if I am making a silly mistake, but I would deeply appreciate if anyone could help me. I tried a few hacky fixes, as I talked about above, but nothing has worked.
EDIT: I am also aware of this thread here, but this doesn't make any sense to my current issue as I am not doing any of this path stuff that they are talking about.
Warm Regards,
GeekyOmega
I fixed the issue. For future readers, this is a very common problem, I think. I placed the .dll in the debug folder of my Visual Studio project. Visual Studio couldn't see the .dll file otherwise. After this, the program fired up as expected and ran. If this does not fix the issue for you, then I suggest something else could be going on.

fstream and ostrstream is undefined

HI,
I am converting my project from vc6 to latest using vs 2010. I get problem on compiling my code
Error 931 error C2065: 'ostrstream' : undeclared identifier
1100 IntelliSense: identifier "fstream" is undefined
I have included the required files as told in Google
#if ! defined(_FSTREAM_)
#include <fstream>
#endif
#if ! defined(_STRSTREAM_)
#include <strstream>
#endif
When i press F12 on the fstream or ostrstream it takes to the respective files where these class are defined. Is there any other includes i have to do, i have been searching for this for long time with no luck :(
Thanks
Arvind
Add these to your library list:
#include <stdio.h>
using namespace std;
Forget all the preprocessor stuff; it's redundant at best (the files in questiion will have reinclusion guards) and at worst an error (you're assuming the #defines used, which are arbitary). Just use code like this:
#include <fstream>
#include <strstream>
Also note that the strstream header is deprecated. You should use sstream instead, but note the newer classed in this file word differently to the deprecated ones).
Without using namespace std; as its good practice.
#include<fstream>
std::fstream fileio;
Where the fileio is the object created by fstream. The fstream class needs to be created using the std keyword as it is part of the standard library.
Hence using : using namespace std; in the code makes it work.
NOW using using namespace std; can cause conflicts in shared libraries.
#include <fstream>
using namespace std;
fstream fileio;
What worked for me was a combination of two answer. After some trial and error, fstream squiggly red lines vanished when I added,
#include <fstream>
using namespace std;

Question about CImage save function

I have encountered a problem while saving the CImage class data. The following are my brief set up:
#include "stdafx.h"
#include <cstring>
#include <atlimage.h>
#include <vector>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CImage myimage;
myimage.Create(100,100,24);
// save an image in BMP format
string impath = "image1.bmp";
myimage.Save((LPCTSTR)impath.c_str());
return 0;
}
I have run it as a WIN32 console application under VC++ 2008. There is no error. However, after the program done, there is NO file created under my directory. I have run this code on two machines, and the results are the same.
Thank you very much for your time and effort.
Maybe you are just looking in the wrong place for the file. Give an absolute path. The relative path is to the current working directory of the process. If you run from the debugger, it could be the output directory of the executable, the project directory or somewhere else.
Also, instead of the cast use the _T macro on c_str() -- in case it needs a conversion.
If you still can't figure it out, get PROCMON (free utility from Microsoft). Run it and filter the output so it's just looking for file with path contains "image1.bmp" -- it will tell you all file operations that were tried with that path and what happened.
myimage.Save((LPCTSTR)impath.c_str());
1) check result
2) why did you use type casting? this can mask the bug, e.g. if you use UNICODE build (default one in VS2008), filename will be invalid

Resources