android is a kind of linux,and it must support the posix.But,when it seems not support the syscall,open().
Here is the code for testing,and i compile it via NDK:
#include <unistd.h>
#include <stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
void main(){
int fd;
char pathname[128] = "/data/pwrite.txt";
fd = open(pathname, O_WRONLY);
if(fd==-1){
printf("open fail.\n");
}
perror("/data/pwrite.txt");
}
and the following is the prompt that comes from android:
kaiwii#ubuntu:~$ adb shell /data/pwrite/test1
open fail.
/data/pwrite.txt: No such file or directory
I think that the problem is in the flags - you only use O_WRONLY. But if a file does not exist you should also create it using O_CREAT flag. So if a file does not exist you should call:
fd = open(pathname, O_WRONLY | O_CREAT);
I think the problem is not the syscall open() but the fact that your are trying to access /data. This folder is accessible only for rooted mobiles or in the emulator. Have you tried to put the file into the /sdcard folder?
Related
I am trying to test a very basic c++ code in CppDroid app to create a text file on my Android phone (Huawei P20pro). But it doesn't seem to work. I think the problem is with the file directory. Can someone let me know how to get the real full directory of a file/folder in Android phone?
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream NewFile;
NewFile.open("/Internal Storage/CppDroid/projects/project_dec23a/Gyrocopter.txt");
NewFile << "This is a file about a gyrocopter. \n";
NewFile.close();
return 0;
}
I have 3 files,
1) q5.c
2) q5.h
3) q5fun.c
The contents of each file is listed below,
q5.c
#include <stdio.h>
#include "q5.h"
int number;
main() {
number = 2;
printf("%d\n",fun(5));
printf("%d\n",number);
}
q5.h
extern int number;
int fun(int);
q5fun.c
#include "q5.h"
int fun(int x) {
return x + number++;
}
On linux, code works fine. I am trying to make my platform mobile on a ground vehicle and I wanted to do such computations on my phone.
I installed CCTools and the GCC add-on to it,
When I compile the main program "q5.c", the compiler throws up saying "undefined reference to 'fun'. What is the terminal command am I suppose to execute?
In unix platform I run "cc q5.c q5fun.c"
Under android platform, what am I suppose to run?
Okay seems I can compile and produce the output the way I want. Now the problem is that a.out file does not have execute permission. Any quick tips on how to solve?
--
Execution from sdcard disabled by default for non-rooted devices. Copy your file to terminal home directory (it defined as /data/data/com.pdaxrom.cctools/root/cctools/home), change permission and run it:
cp myprogram ~/
chmod 755 ~/myprogram
~/myprogram
Also, you can use Makefile to compile two or more sources files.
I have an Android application that use JNI. In JNI I used C++ STL library to output some data into a text file.
This is the snippet code.
#include <iostream>
#include <fstream>
float mydata[4] = {0.0f};
ofstream file;
file.open("Data.txt", ios::app);
// For example
file<<mydata[1];
file.close();
when I ran ndk-build, I did not face any errors and the program seems run properly. But I did not found any text file in my Application directory.
does anybody know what could be the issue? Please help.
Thanks
Your code works, but you apparently try to access a path which is forbidden.
Firstly, be sure that your Android application asks for storage permissions.
Secondly, try to replace "Data" by an absolute path you know you have access to (e.g. "/sdcard/Pictures/Data.txt").
Also, as a better practice, you should test if the file has been opened before writing to it.
I've managed to compile android from source and install on my phone. I should probably say from the outset I'm not a c++ developer but I had hoped I would be able to work out how to output the data held in a variable to a file.
After a bit of googling I came up with the following code:
#include <iostream>
#include <fstream>
using namespace std;
ofstream myfile;
myfile.open ("/data/wav.raw");
myfile < mMixBuffer;
myfile.close();
frameworks/base/services/audioflinger/AudioFlinger.cpp: In member function 'virtual bool android::AudioFlinger::MixerThread::threadLoop()':
frameworks/base/services/audioflinger/AudioFlinger.cpp:1769: error: 'ofstream' was not declared in this scope
frameworks/base/services/audioflinger/AudioFlinger.cpp:1769: error: expected ';' before 'myfile'
frameworks/base/services/audioflinger/AudioFlinger.cpp:1770: error: 'myfile' was not declared in this scope
after this didn't work I tried which again gave compilation errors:
int fd;
fd = ::open("/data/wav.raw",O_WRONLY | O_APPEND);
::write(fd,mMixBuffer,minBufferSize);
::close(fd);
frameworks/base/services/audioflinger/AudioFlinger.cpp: In member function 'virtual bool android::AudioFlinger::MixerThread::threadLoop()':
frameworks/base/services/audioflinger/AudioFlinger.cpp:1770: error: '::open' has not been declared
frameworks/base/services/audioflinger/AudioFlinger.cpp:1770: error: 'O_WRONLY' was not declared in this scope
frameworks/base/services/audioflinger/AudioFlinger.cpp:1770: error: 'O_APPEND' was not declared in this scope
If it's not clear from the above I'm trying to append the contents of variable mMixBuffer with size minBufferSize to a file /data/wav.raw. Does anyone have any idea how I can achieve this?
/data/ directory can not be modified. You won't be having the permissions for the same. Try /mnt/sdcard (or) /data/data/.
I have a directory mounted by fuse,and i am intented to use lstat() against this dirctory.But,when i run the following code,it just wait there and prompt nothing.
And by the way,i run the fuse in the android emulator.
the code is:
#include <sys/stat.h>
#include <stdio.h>
void main(){
printf("new test!!!");
char *path="/data/pwrite/test_12/";
struct stat *stbuf;
int res=12;
res=lstat(path, stbuf);
printf("%d",res);
}
And,"/data/pwrite/test_12/" is the fuse-mounted directory.What's more,when i try another dicrtory that share the same parent directory but not mounted by fuse,like /data/pwrite/test_13/,it works!
so,i definely sure it is leaded by fuse.But,i'm even more confused that whether it is due to the conflict between fuse and android.
Any idea?thx
Try without the bugs and see if that works better.
#include <sys/stat.h>
#include <stdio.h>
void main(){
printf("new test!!!\n");
const char *path="/data/pwrite/test_13/";
struct stat stbuf;
int res=12;
res=lstat(path, &stbuf);
printf("%d\n",res);
}
why it not work?because there runs several fuse-deamon currently.