I want to change size of *.txt file, which is created before. For this i use chsize.
Code:
#include <iostream>
#include <stdio.h>
#include <io.h>
using namespace std;
int main()
{
FILE *wfile;
wfile = fopen("test.exe", "a");
chsize(wfile, 1024); //error is here
file.close();
return 0;
}
And here goes the error:
[Error] invalid conversion from 'FILE* {aka _iobuf*}' to 'int'
[-fpermissive]
Q: What is wrong here? I opened the file with fopen right how it explained in the internets.
Q2: I love Windows as a OS, but I don't want to learn Windows API for C++ or something like that. Is there a option to use something portable?
First parameter for chsize is file descriptor not FILE*.
You can use function "fileno".
How can I convert a file pointer ( FILE* fp ) to a file descriptor (int fd)?
Related
Can't compile this small code on Linux:
#include <fstream>
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
char fileName[512];
sprintf(fileName, "test");
ofstream iriFile(string(fileName));
iriFile<<",";
return 0;
}
I am compiling like this: g++ test.cpp and am getting this:
test.cpp:12:11: error: invalid operands of types
‘std::ofstream(std::__cxx11::string) {aka
std::basic_ofstream(std::__cxx11::basic_string)}’ and
‘const char [2]’ to binary ‘operator<<’ iriFile<<",";
What might be the reason?
Ok, the solution is to remove implicit string() creation:
string sFileName(fileName)
ofstream iriFile(sFileName);
First of all you do not need to explicitly convert const char * to std::string there is std::ifstream constructor for it:
std::ofstream iriFile(fileName);
but if you want to be extra safe and verbose use proper C++ then:
std::ofstream iriFile( static_cast<std::string>(fileName) );
not C style cast.
As you pointed out, removing the explicit string creation fixes it.
It could be worth to add that this can also be fixed for types with explicit constructors by using list initialization, like so:
ofstream iriFile(string{sFileName});
I am new to C++ coding using linux.Hence, my apologies if my question is trivial.
I need some help regarding calling some script/executable from inside a cpp file.
I downloaded few libraries (Blas, Lapack, libtsnnls-2.3.3). Configured and made executable. This executable was created when I configured and compiled libtsnnls-2.3.3.
I can call from command line:
cd /home/dkumar/libtsnnls-2.3.3/tsnnls
./genb_test
Now, I want to call the same command from a cpp file. It's something similar to "HelloWorld.cpp"
My Attempt (modified based on suggestion of #Biffer #timrau:
// 'Hello World!' program
#include <stdio.h> /* defines FILENAME_MAX */
#include <cstdlib> /* MODIFIED std::system */
#include <iostream>
#ifdef _MSC_VER
#include "direct.h"
#define GetCurrentDir _getcwd // window ??
#else
#include "unistd.h"
#define GetCurrentDir getcwd
#endif
int main()
{
std::cout << "Hello World!" << std::endl;
// const char *ParentFolder = "/home/dkumar/All_Matlab_Codes_DKU";
const char *ParentFolder = "/home/dkumar/libtsnnls-2.3.3/tsnnls/";
int res3 = chdir(ParentFolder);
// exceuting the command('./genb_test')
std::system('./genb_test');
return 0;
}
I get the following errors:
HelloWorld.cpp:36:10: warning: character constant too long for its type [enabled by default]
system('./genb_test');
^
HelloWorld.cpp: In function ‘int main()’:
HelloWorld.cpp:36:23: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]
system('./genb_test');
^
In file included from /usr/include/c++/4.8/cstdlib:72:0,
from HelloWorld.cpp:4:
/usr/include/stdlib.h:717:12: error: initializing argument 1 of ‘int system(const char*)’ [-fpermissive]
extern int system (const char *__command) __wur;
In c++ ' is used for characters ('a') and " is used for strings ("aaaa").
If you edit system('./genb_test') to be system("./genb_test") it might work.
You have single ' when calling your script. You want double "
#include <cstdlib> /* MODIFIED std::system */
int main()
{
// executing the command("./genb_test")
std::system("./genb_test");
return 0;
}
there is a function named system which could start a shell process to run your script, to do this, you need first include cstdlib header and then call the system function in your code
example
#include <cstdlib>
int main()
{
system("./myscript.sh");
}
The function system need a parameter of type std::string or a C-style string, so you will need the double quotation mark
I am having a very strange issue with stat.h
At the top of my code, I have declarations:
#include <sys\types.h>
#include <sys\stat.h>
And function prototype:
int FileSize(string szFileName);
Finally, the function itself is defined as follows:
int FileSize(string szFileName)
{
struct stat fileStat;
int err = stat( szFileName.c_str(), &fileStat );
if (0 != err) return 0;
return fileStat.st_size;
}
When I attempt to compile this code, I get the error:
divide.cpp: In function 'int FileSize(std::string)':
divide.cpp:216: error: aggregate 'stat fileStat' has incomplete type and cannot be defined
divide.cpp:217: error: invalid use of incomplete type 'struct stat'
divide.cpp:216: error: forward declaration of 'struct stat'
From this thread: How can I get a file's size in C?
I think this code should work and I cannot figure out why it does not compile. Can anybody spot what I am doing wrong?
Are your \'s supposed to be /'s or am I just confused about your environment?
UNIX MAN page:
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int stat(const char *restrict path, struct stat *restrict buf);
If you're on Windows (which I'm guessing you might be because of the \'s), then I can't help because I didn't even know that had stat.
Goodnight to everyone, I'm trying to parse an .h file so I can have a small console frontend to change its values, but when I try to use strncmp with a string read from a file and a string defined in code to compare with the file string I get a strange error from the compiler that I cant resolve, here is my source code:
//Test to basic file operations
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
int main (void){
string line;
ifstream myfile("PIDconfig.h");
if(myfile.is_open()){ //if file is open
while(myfile.good()){
getline(myfile, line);
if(strncmp(line, "static float", 12) == 0){
cout << line << endl;
}
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
And the error that I get:
tiago#tiago-laptop:~$ g++ file.cpp
file.cpp: In function ‘int main()’:
file.cpp:17: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int strncmp(const char*, const char*, size_t)’
If some one could help me I would be very glad, I have already searched StackOverflow but I didnt found anyone with the same problem, almost all strncmp problems use arrays to store their strings and as far as I went, no one was having a problem using it and file I/O.
std::string overloads operator==. You can simply compare two std::string object using ==.
Also, your input loop is incorrect.
the problem is that strncmp() function overloaded for strncmp(const char*, const char*, int)
but you want to call it by strncmp(string, string, size_t)
you must convert string to const char* with
c_str()
for example
string str = "Hello";
char * arr = str.c_str().
you get it?
if(strncmp(line.c_str(), "static float", 12) == 0){
should work
The problem is that you're reading data from the file as a C++ string, and the strncmp function works on C style strings. To fix this, you can either extract a raw C style string from the C++ string using .c_str(), or you can use the C++ string's .compare function:
line.compare(0, 12, "static float")
Okay, I am having trouble with the following piece of code (in a header file):
#ifndef XML_H_INCLUDED
#define XML_H_INCLUDED
#include "libxml/parser.h"
#include "libxml/xmlwriter.h"
#include <string>
class XmlFile{
public:
XmlFile(string filename){
file = xmlParseFile(filename);
}
xmlDocPtr file; //Pointer to xml file
};
#endif // XML_H_INCLUDED
The file is including in the main source file (but is not accessed, so its contents are not important).
I keep getting the following error (In Codeblocks):
error: cannot convert 'std::string' to 'const char*'
for argument '1' to 'xmlDoc* xmlParseFile(const char*)'|
I have run into this many times, and it is driving me crazy.
I would prefer not to use vectors if possible (adds another step in initializing the function.
What am I doing wrong? I've tried looking this up, but have not found any satisfactory answers.
Thanks in advance.
file = xmlParseFile(filename.c_str());