I get this error code in VS: 0x80070002 c++ - c++

When i try to run a code with the function "strtok" in it i get the error code 0x80070002. I included cstring, cctype, string.h and i also tried using /DEBUG FULL in Properties - Linker - Debugging, like a few other posts said but it still doesn't work. Any clues why VS doesn't work with strtok? I also tried reinstalling VS and running a simple code like this:
#include <iostream>
#include <cstring>
#include <cctype>
#include <string.h>
using namespace std;
int main()
{
char s[100], * p;
cin.getline(s, 100);
p = strtok(s, " ");
cout << p;
return 0;
}
The desired behaviour would be to show me the first word of s. Even when i try to run the code at https://en.cppreference.com/w/cpp/string/byte/strtok i get the same error.

There are two methods to meet your needs;
Add _CRT_SECURE_NO_WARNINGS in Properties->C/C++->Preprocessor->Preprocessor Definitions.
Use strtok_s instead of strtok:
int main()
{
char *buf;
char s[100], *p;
cin.getline(s, 100);
p = strtok_s(s, " ", &buf);
cout << p;
return 0;
}

Related

6031: return value ignored (getchar()) In visual studio 2019

It does not affect my code, but I have never seen such issue until I updated my visual studio. I don't know if thats connected, but I am very confused why is there an issue.
#include <iostream>
#include <string>
#include <array>
using namespace std;
int main() {
const int SIZE = 3;
array<string, SIZE> names = { "S","A","W" };
array<string, SIZE>::iterator it;
cout << "names: \n";
for (it = names.begin(); it != names.end(); it++)
cout << *it << endl;
getchar();
return 0;
}
When visual studio was updated, they added a [[nodiscard]] attribute to getchar. This tells the compiler to warn the user whenever the return value of a function is ignored. You can find out more here: https://en.cppreference.com/w/cpp/language/attributes/nodiscard
In this case, because you're using getchar just to prevent the window from closing, you don't need the return value, so you can ignore this warning.
We can silence the warning by explicitly ignoring the return value:
(void)getchar(); //Explicitly ignore return value
My personal fix in this situation, to make a pause in the console, is doubling it like this:
getchar();getchar();

stop recursion for recursive_directory_operator c++

So let's say i have this code that looks for documents of mine and prints the path to them.
#include <iostream>
#include <experimental/filesystem>
#include <string>
#include <stdio.h>
#include <Shlwapi.h>
using namespace std;
string extensions[3] = { ".doc", ".docx", ".txt" }; // things to look for
string ignoredirs[2] = { "Windows", "Program Files", } // and other ones that i was too
// lazy to write in there
using namespace std::experimental::filesystem;
path yee;
void main()
{
for (recursive_directory_iterator i("c:\\"), end; i != end; ++i)
if (!is_directory(i->path()) && i->path().has_extension()) // checks if the file
// even has an extension
{
for (int x = 0; x <= 3; ++x)
if (i->path().extension().string() == extensions[x])// checks if the
// extension is equal
// to current
// extension in loop
cout << "found document at :" ;
cout << i->path().string() << endl; // print out the path
}
}
And I would like to not iterate to directories in ignoredir[] because it takes ages to find my docs on the filesystem.
I saw this code from cppreference.com.
Could someone explain me the the code and/or how to use it in my use case?
Pr could you submit a better solution than that?
Ps. I don't want to use boost in this program, just to see how it works in experimental::filesystem
Proposed solution:
#include <iostream>
#include <experimental/filesystem>
#include <string>
#include <stdio.h>
#include <Shlwapi.h>
#include <set>
using namespace std;
set<string> extensions = { ".doc", ".docx", ".txt" }; //things to look for
set<string> ignoredirs = { "Windows", "Program Files" }; //and other ones that i was too lazy to write in there
using namespace std::experimental::filesystem;
int main()
{
for (recursive_directory_iterator i("c:\\"), end; i != end; ++i)
{
if (!is_directory(i->path()) && i->path().has_extension()) // checks ifthe file even has an extension
{
if (extensions.find(i->path().extension().string()) != extensions.end())
cout << "found document at :";
cout << i->path().string() << endl; //print out the path
}
if (ignoredirs.find(i->path().filename().string()) != ignoredirs.end())
i.disable_recursion_pending();
}
}
Explanation:
i->path().filename() actually return directory name, when the same directory name is in set<> then i.disable_recursion_pending(); is called. When this one is called recursive_directory_iterator i omit directory with i->path().filename() name.
set<string> was used to get rid internal for loop that requires size of table, which is error prone. Also performance gain and code simplification
Notice:
On windows directory names should be compared case insensitive. Also Windows 64bit have "Program Files" and "Program Files (x86)" and both need to be listed in ignore list (or string comparison need to be improved)

C++ Access violation reading location when using strcpy

The command to compile in Ubuntu Terminal is g++ scrap.c -std=c++11
I looked at several other questions with the same error but none use strcat which is where the exception occurs for me.
I am trying to copy all chars from test1 into temp[] in chunks of 512 chars at a time, but I am getting an exception Access violation reading location 0x....
Another thing I'm trying to do is load temp into an item of fileArray[] but the strcpy at the bottom of my code is giving me no suitable conversion from str to "char *" exists.
I tried hard to give a "complete, minimal, verifiable example" since admins seem to be pushing that really hard right now; please let me know if I did it right here. You should just be able to copy my code and compile it with g++ to check it.
scrap.c (Updated):
//#include <stdafx.h>
//#include "stdafx.h"
#include <stdio.h> /* defines FILENAME_MAX */
#include <assert.h>
#include <iostream>
#include <fstream>
#include <cstring>
//#include <filesystem>
#include <experimental/filesystem>
#include "dirent.h"
//#include <direct.h>
using namespace std;
int main() {
string test1 = "This is test1.txt...This is test1.txt...";
std::vector<std::string> fileArray[10];
string temp = "";
for (int a = 0; a < 10; a++) {
int block = 512 * a;
int currentBlockPosition = 0;
while (currentBlockPosition < 512 && (currentBlockPosition + block) < test1.size()) {
temp += test1.at(block + currentBlockPosition);
cout << "block + currentBlockPos: " << block + currentBlockPosition << endl;
cout << "current char: " << test1.at(block + currentBlockPosition) << endl;
currentBlockPosition++;
}
temp = "";
cout << "temp: " << temp << endl;
cout << "a: " << a << endl;
//fileArray[a] = temp; // no operator "=" matches these operands ... operand types are: std::vector<std::string, std::allocator<std::string>> = std::string
}
}
Found the solution.
The access violation error had to do with me using a char [] instead of a string.
Using fileArray->push_back(temp); turned out to be the proper syntax instead of fileArray[a] = temp.
Also, I had to add in the second condition in the while() to avoid the out of bounds error.

Assertion failure in std::ispunct in a simple program

I'm using the book C++primer by Stanley B.Lippman and this error is caused by the solution of Excersise 3.2.3 test 3.10.It requires that write a program that reads a string of characters including punctuation and writes what was read but with the punctuation removed.
here's the code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string s;
cout << "Please input a string of characters including punctuation:" << endl;
getline(cin, s);
for (auto c : s) {
if (!ispunct(c))
cout << c;
}
cout << endl;
return 0;
}
when I run this code in Visual studio 2017 it shows this:
Debug Assertion failed.
Expression:c>=-1&&c<=255
For information on how your program can cause an assertion failure,see the Visual C++ documentation on asserts.
why it shows like this? I can't understand.
Although the assertion failure you get is due to a bad call to std::ispunct() (you should iterate over the string with an unsigned char), the proper solution would be to use std::iswpunct:
#include <iostream>
#include <string>
#include <locale>
#include <cwctype> // std::iswpunct
int main()
{
std::wstring s;
do {
std::wcout << "Please input a string of characters including punctuation:\n";
} while (!std::getline(std::wcin, s));
for (auto c : s) {
if (!std::iswpunct(c))
std::wcout << c;
}
std::wcout << std::endl;
}
On a Windows platform, the conjunction of std::wstring1 and std::iswpunct will let you handle Chinese characters right. Note that I assumed your system locale is "zh_CH.UTF-8". If it is not, you'll need to imbue your streams.
1) see this excellent answer about the difference between string and wstring.

Not able to make carriage return work on linux

i'm having problem with this code :
#include <iostream>
#include <math.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <stdio.h>
using std::cout; using std::cerr;
using std::cin; using std::string;
using std::endl;
int main(int argc,char* argv[])
{
for(int x = 0; x <= 2013; x++)
{
cout << "Iteration: "<< x << "\r";
cout << "";
}
return 0;
}
i need my code to print "Iteration: 0" and than just refresh that 0 to 1,2,3,4.... everything on one console line. I used the carriage return but it doesn't work,the line are printed one after another as when i use "\n". The enviroment is linux 64 bit. The IDE is eclipse 8.01.
You have to put it at the beginning of the line:
cout << "\rIteration: "<< x;
EDIT: I have tested this modification of the original OP's code and it prints what he wants. Also, Oh dear look what I've found.
Also, as suggested by #Wintermute, you can do the following inside the for loop, for better visualization:
cout << "\rIteration: "<< x << std::flush;
sleep(1);