Visual Studio fopen_s error - visual-studio

I tried to open a file with fopen_s in Visual Studio 2017
the code looks like
#include<stdio.h>
int main() {
FILE*fp;
fopen_s(&fp, "input.txt", "r,css=UTF-8");
fclose(fp);
return 0;
}
then i got a run time error which looks like:
File:minkernel\crts\ucrt\inc\corecrt_internal_stdio.h Line:684
Expression:("Invalid file open mode", 0)
For information on how your program can cause an assertion failure,
see the Visual C++ documentation on asserts

The flag is called ccs not css.
#include<stdio.h>
int main() {
FILE*fp;
fopen_s(&fp, "input.txt", "r,ccs=UTF-8");
fclose(fp);
return 0;
}

Related

MPI Init causes crash in Visual Studio 2010

I am trying to get an MPI program to run, however it appears to crash on the MPI_INIT call.
Visual studio merely says the exe "has triggered a breakpoint", but doesn't tell me anything else. I am trying to run with 4 processes.
int main(int argc, char **argv) {
int testNumber;
long wcTime, totalWcTime;
float cpuTime, totalCPUTime;
_totalTextLength = 0;
totalWcTime = 0;
totalCPUTime = 0;
MPI_Init(&argc, &argv);
The call stack is as follows:

Phantom syntax errors in Visual C++ 2010 Express

I just started learning C++, and I'm getting weird 'phantom' syntax errors in Visual C++ 2010 Express.
There are red lines under seemingly random pieces of code, and when I hover my cursor over them it shows errors that seem to make no sense at all. However, when I hit F5 the program compiles and runs successfully.
It's hard to learn C++ like this because I can't quickly distinguish between real syntax errors and 'fake' ones.
The syntax errors:
http://i.stack.imgur.com/O0UbD.png
The program:
#include <iostream>
#include "conio.h"
#include "windows.h"
class test2
{
public:
int i;
};
class testc
{
public:
test2 hi;
};
int main()
{
testc hello;
hello.hi.i = 23;
std::cout << hello.hi.i << "\n";
system("pause");
}
I reinstalled entire Visual Studio, not just Visual C++. Now it works correctly.

curl_easy_escape not working from C++ dll

I am having trouble using the curl_easy_escape function in a C++ dll. If I create a console application, curl_easy_escape works as expected.
cURL curl-7.28.1-devel-mingw32
Visual Studio 2010
Console Application (working as expected)
int _tmain(int argc, _TCHAR* argv[])
{
CURL *curl;
char *data1;
char *data2;
int data2len;
curl = curl_easy_init();
if (curl)
{
data1 = curl_easy_escape(curl, "this is a string with spaces", 0);
printf("\r\ndata1 = %s\r\n", data1);
data2 = curl_easy_unescape(curl, data1, 0, &data2len);
printf("\r\ndata2 = %s\r\n", data2);
curl_easy_cleanup(curl);
}
printf(“\r\nPress any key to continue\r\n”);
return 0;
Output
data1 = this%20is%20a%20string%20with%20spaces
data2 = this is a string with spaces
Press any key to continue
C++ DLL
Visual C++ > Win32 > Win32 Project
Application type = DLL (all other options as default)
Linker > Input > Additional Dependencies, added libcurl.lib
extern "C"
{
__declspec(dllexport) int * curl_test(char *sBuf)
{
CURL *curl;
curl = curl_easy_init();
if (curl)
{
// The following line does not work
//strcpy(sBuf, curl_easy_escape(curl, "this is a string with spaces", 0));
// This line does work
strcpy(sBuf, "this is a string with spaces");
curl_easy_cleanup(curl);
}
return 0;
}
}
The dll is used in a C program and calls curl_test with char ptr[256]. With curl_easy_escape in place, the LoadLibrary call fails:
Error: C interpreter run time error: Error -- File error : LoadLibrary(mydll.dll) failed : The specified procedure could not be found.
Note that this is on the LoadLibrary, so the curl_test function has not been called yet.
I suppose I can find some URL encode/decode functions, but would prefer to use what cURL already offers. I also have the same issue with the curl_easy_strerror function.
Could it be the ‘const char *’?
Update
Ideally I would like to use curl_easy_escape/curl_easy_unescape, but as a workaround I decided to implement my own URL encoding/decoding in C. I found efficient, well written encoding/decoding functions at http://www.geekhideout.com/urlcode.shtml

c/c++: thread to decrement a variable every 1 second? (in windows)

Well, I was planning to do this:
int seconds = 90;
void *DecreaseSeconds(){
while (seconds>-1)
{
seconds--;
sleep(1000);
}
return NULL;
}
int main(int argc, char *argv[]){
int threadid= pthread_create(&threads[i], NULL, DecreaseSeconds, NULL);
pthread_join(threadid, NULL);
}
Yet I get this dreadful thing when I try to compile on Visual Studio 2008
fatal error C1083: Cannot open include file: 'pthread.h': No such file or directory
I want a way to translate this to windows or make Visual Studio accept my posix thread.
Look up RTL function _beginthreadex.
There is no POSIX thread support on Win32. You need to use Win32 threads or an abstraction that supports both.

Keeping Console Window Open in Visual Studio (C)

I usually use XCode but was having a problem opening a file with this code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
int main(void )
{
printf("Hello");
FILE *filePtr;
filePtr = fopen( "test.txt", "r" );
if (filePtr == NULL)
{
fprintf(stderr, "Can't open \"test\"\n");
exit(EXIT_FAILURE);
}
else
{
int x;
printf("File open successful\n");
/* read one character at a time until EOF is reached */
while ((x = fgetc(filePtr)) != EOF)
{
//printf("%c", x);
fprintf(stderr, "%x\n",x);
}
}
fclose(filePtr);
system("pause");
return EXIT_FAILURE;
}
The console window closes so fast and at the bottom bar of VS it says: "'C_test.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcr90d.dll'
The program '[1116] C_test.exe: Native' has exited with code 1 (0x1)."
What does that mean?
Also, can anyone point me to good VS starting points / tutorials?
See also How to keep the console window open in Visual C++?
The reason you can't see it is because there is no possibility for your program to pause during execution. In Visual Studio the typical behavior is to close the console window the second the program has completed its execution.
The bottom bar is telling you that the program complete and what the return value was (1 in this case).
What I would also do is add code right before the exit point of the program with #ifdefs:
#ifdef VS_DEBUG
fgetc(STDIN);
#endif
Now your program will pause when it's done and wait for a keypress then close the window.
I'm sure there is also a way in the project settings to prevent the closing, I've never looked myself.
I generally leave a breakpoint on the closing brace of main, so that the output of my window is visible while debugging, but Visual Studio will keep the console window open if you start the program without debugging (Ctrl+F5). Alternatively, you could simply ask for input, #MadcapLaugher's fgetc(STDIN); is probably your best bet - though I would add a prompt: "Press any key to continue... "
#include<stdio.h>
int main()
{
printf("Hello World from visual Studio \n");
//to prevent console window temination
system("pause");
return 0;
}

Resources