I'm having problems with st_size of stat struct in a simple program for Raspberry Pi.
I write the code in Eclipse C/C++ IDE (CDT) with a Macbook, and then compile it in the Raspberry Pi. I didn't have problems until I started using fstat and stat structure.
I test this code in Raspberry Pi and it work fine.
#include <stdio.h>
#include <arm-linux-gnueabihf/sys/stat.h>
#include <unistd.h>
int main(void) {
FILE *pFile;
struct stat buf;
pFile = fopen("File.bin","rb");
if ( pFile != 0 ) {
fstat(fileno(pFile), &buf);
printf("File size: %d\n", buf.st_size);
//printf("File size: %d\n", buf.stat);
fclose(pFile);
}
return 0;
}
The problem is when I tried to use buf.st_size the Eclipse throws Field 'st_size' could not be resolve. Instead, the Eclipse autocomplete with '.stat' like the commented line (buf.stat).
So far, I added in "C/C++ General -> Paths and Symbols -> Includes" the path where I copied the includes "stat.h" obtained from the "Raspberry Pi".
I can't make "Field 'st_size' could not be resolve" problem go away.
Any suggestion would be appreciate!
Thank you for your time
Related
I am running just a little code using libsndfile, in the emscripten environment
#include <iostream>
#include <sndfile.h>
int main()
{
SF_INFO info;
const char * path = "~/data/somefile.wav";
SNDFILE* sf = sf_open(path,SFM_READ, &info);
if(sf == NULL)
{
std::cout<< sf_strerror(sf) << std::endl;
return 1;
}
std::cout<<info.samplerate<<std::endl;
std::cout<<"Hello world" << std::endl;
}
So ideally if I run this with normal cmake (Apple Clang compiler) everything works fine, the samplerate and hello world are printed, but when I run this with emcmake cmake (em++ compiler) and run the compiled node main.js file it says System error: no such file or directory. Who can help me with this? Who has experienced such thing?
So I figured it out.
The problem is that Emscripten has its virtual file environment. So if you want this file to be uploaded and later be seen in compiled .js file, you need to add compile flag --preload-file <FILE_PATH> , after that the file with given path will be recognized by emscripten environment.
I am working on Windows and I am trying to write an array into a Ubuntu device using C++ in Visual Studio 2019. Here's a sample of my code:
int Run_WriteCalibTable(char *pcIPAddress, int iNumArgs, float *fArgs, int *iAnsSize, char *sAns)
...
...
...
char pcFolderName[256];
char pcFileName[256];
sprintf(pcFolderName, "%s\\%s",pcSavePath, pcUUTSerialNumber);
sprintf(pcFileName, "%s\\calib_rfclock.conf",pcFolderName);
// WRITE TABLE ON PC
FILE *pFileW;
pFileW = fopen(pcFileName,"wb");
fwrite(&CalibTable, sizeof(char), CalibTable.hdr.v1.u32Len, pFileW);
fclose(pFileW);
}
return 0;
However, I keep having this pop-up from Microsoft Visual C++ Debug Library that says:
Debug Assertion Failed:
Program:...
File: f:\dd\vctools\crt_bld\sefl_x86\crt\src\fwrite.c
Line: 77
Expression: (stream != NULL)
...
I found this thread and I tried logging in as root on my Ubuntu device. I also tried:
mount -o remount,rw /path/to/parent/directory
chmod 777 /path/to/parent/directory
And I can also create/edit manualy any file in the directory I'm trying to write into with my code, but I get the same error when running it.
Anyone knows what could cause this? I think it could be on the Windows side, but I don't know what I am doing wrong. Thanks a lot in advance.
You never check that opening the file succeeds - and it most likely fails, which is why you get the debug pop-up. Your use of \ as directory delimiters may be the only reason why it fails, but you should check to be sure.
I suggest that you use std::filesystem::path (C++17) to build your paths. That makes it easy to create paths in a portable way. You could also make use of a C++ standard std::ofstream to create the file. That way you don't need to close it afterwards. It closes automatically when it goes out of scope.
Example:
#include <cerrno>
#include <cstring>
#include <filesystem>
#include <fstream>
int Run_WriteCalibTable(char *pcIPAddress, int iNumArgs, float *fArgs,
int *iAnsSize, char *sAns)
{
...
// Build std::filesystem::paths:
auto pcFolderName = std::filesystem::path(pcSavePath) / pcUUTSerialNumber;
auto pcFileName = pcFolderName / "calib_rfclock.conf";
// only try to write to the file if opening the file succeeds:
if(std::ofstream pFileW(pcFileName, std::ios::binary); pFileW) {
// Successfully opened the file, now write to it:
pFileW.write(reinterpret_cast<const char*>(&CalibTable),
CalibTable.hdr.v1.u32Len);
} else {
// Opening the file failed, print the reason:
std::cerr << pcFileName << ": " << std::strerror(errno) << std::endl;
}
...
}
Good evening everyone, please I'm writing a Library management application in c++ using virtual studio 2012. I had already writing some codes using Dev c++ it worked but when I switched to visual studio it gives error. It involves creating folders and checking if the folders were actually created. That is using dir and mkdir.
Windows and Linux (POSIX) don't support the same API for most file system functions. You can use Microsoft's platform-specific APIs like CreateDirectory() or use the POSIX-like versions like _mkdir().
If you have a more recent C++ compiler / standard library, you can use the experimental filesystem library that is slated to become part of standard C++, perhaps as early as C++17. If not, you can use Boost.Filesystem from which the pre-standard experimental library was drawn.
Here's a complete, minimal example using Boost.Filesystem, which will work on both Windows and Linux without modification:
#include <iostream>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
int main()
{
if( !fs::exists( "my_dir" ) )
{
if( fs::create_directory( "my_dir" ) )
{
std::cout << "Directory created!\n";
}
}
}
See it run: Coliru.
Here's the same code but with std::experimental::filesystem: Coliru.
You would need the appropriate include and linker paths setup in your build system for either of these to work locally. The biggest "gotcha" using the filesystem is that it throws exceptions for a lot of errors by default. You can either setup try/catch blocks at the appropriate places or pass in an error code param to make it return the status there instead.
#include <stdio.h>
#include <windows.h>
int main() {
if (!CreateDirectoryA("C:\\ABC123", NULL))
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
printf("Already Exists");
}else if (GetLastError()== ERROR_PATH_NOT_FOUND)
{
printf("Path not found");
}
}else{
printf("Created..");
}
}
simple function will do.
Thanks alot guys but I found this that solved my problem
#include <iostream>
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
void main( void )
{
if( _mkdir( "\\testtmp" ) == 0 )
{
printf( "Directory '\\testtmp' was successfully created\n" );
system( "dir \\testtmp" );
if( _rmdir( "\\testtmp" ) == 0 )
printf( "Directory '\\testtmp' was successfully removed\n" );
else
printf( "Problem removing directory '\\testtmp'\n" );
}
else
printf( "Problem creating directory '\\testtmp'\n" );
int a;
cin >> a;
}
the cin >> a; is just to keep the output screen so I can see the result
I'm using tinyxml through openframeworks which uses cstdio for file access. I can see the example program quite happily create and write files but there is no delete so my plan is to implement remove, but after trying to run this code in my own project it doesn't seem to create a file or notify me of an error.
This code runs as expected on windows, just not on mac osx 10.8.5, no file is generated.
#include <cstdio>
int main(int argc, const char * argv[])
{
bool bClosed = false;
bool bWritten = false;
FILE* testFile;
testFile = fopen(".\\test.xml", "w");
if(testFile)
{
bWritten = fputs("test writing.", testFile);
bClosed = !fclose(testFile);
}
return 0;
}
edit: i now know the file exists as can read from it, i just cant view it in finder, i have hidden files shown, its not found its way into the app's package contents.
On a unix-like system (e.g. Mac OS X and Linux) a Windows path as
".\\test.xml"
should rather be
"./test.xml"
Anyway the simplest solution for this case might just be
"test.xml"
This is the code I have for checking if a file exists in my visual studio 2010 c++ project:
bool GLSLProgram::fileExists( const string & fileName )
{
struct stat info;
int ret = -1;
ret = stat(fileName.c_str(), &info);
return 0 == ret;
}
I am not sure why it returns false for "shaders/color.vert" when that file really exists, and shaders is a folder in my project main folder.
Can you see something wrong?
THanks
Ok, so to illustrate the quirks of running from the IDE here's a little test I did. Hopefully this should help you figure out how relative paths work in VS.
So my folder hierarchy looks like this:
/_Sandbox
_Sandbox.sln
/Debug
_Sandbox.exe
/shaders
color.vert
/_Sandbox
_Sandbox.proj
main.cpp
The code looks as follows:
#include <iostream>
#include <string>
#include <sys/stat.h>
int main(int argc, char* argv[])
{
struct stat info;
std::string path = "shaders/color.vert"; // To not I get the same behavior with "shaders\\color.vert"
int ret = stat(path.c_str(), &info);
ret == 0 ? std::cout << "File found." << std::endl : std::cout << "File doesn't exist." << std::endl;
std::cin.get();
return 0;
}
So if I run this in the IDE, I get "File doesn't exist.", if I run this outside the IDE, I get "File Found". In order for the program to find the shader file from inside VS I have to put the shader folder like so:
/_Sandbox
_Sandbox.sln
/Debug
_Sandbox.exe
/_Sandbox
/shaders
color.vert
_Sandbox.proj
main.cpp
You can however get the code to find the folder from inside and outside the IDE. What you have to do is go to your project's settings. In "Debugging" and change "Working directory" to $(SolutionDir)$(Configuration)\
Hopefully this clears things up for you.