So I have a situation where I am trying to write to an Embedded Linux file (Nitrogen6x board). I can do this manually with echo 1 > /sys/class/gpio/gpio16/value. However, when I try and do this same thing via C code:
FILE *fs;
fs = fopen("/sys/class/gpio/gpio16/value","w")
fputc(1,fs);
fclose(fs);
it doesn't work. It doesn't give any errors or anything, it just doesn't work. Has anyone ever encountered this before? What can I do to fix this. Is there a way to "echo" like I do manually...but do this from code? What is the best way to proceed?
Thanks for all your help!
That's because you are writing "CTRL-A" (character code 1, not the digit '1' that echo sends) to the gpio16/value.
Try:
fputc('1', fs);
If that still doesn't work, it may be that you need a newline as well:
fputs("1\n", fs);
Try using fputc('1',fs);.
echo writes asci that you redirect to the file.
Related
I’m looking for a C/C++ way to execute a shell command and store the output of that command in a std::string or file, without the output being automatically printed to the console.
All approaches I’ve seen can do exactly that, but also print the execution result to the console.
For example with a combination of
FILE* pipe = popen("ls", "r");
and
fgets()
I’m able to do just that, but with the printing to the console.
Is there perhaps a way to redirect the stream’s buffer from std::cout to std::sstream, or has it semething to do with Ubuntu?
Any help is appreciated :)
The 2>&1 part from the comments did it.
I'm running my code and give inputs via a .txt file(./a.out<input.txt ), but it seems it does not recognize end of file.
when I copy the contents in ubuntu it works. I think that files were created in MAC.
EOF is like CTRL+C in terminal so it should stop executing the code but the last command continuously is parsing as input. I can not change the file (It is test file and the format is unchangeable.). Can anyone please help me with this problem?
I have attached my code, but I think that is the file problem.
EDIT: I found the solution. By adding
if(!getline(cin, s)){
break;
}
or similar lines, the problem will be fixed!
I found the solution. By adding
if(!getline(cin, s)){
break;
}
or similar lines, the problem will be fixed!
I have a batch file I need to execute, it has one parameter, If I were to run this script myself I would open up cmd and write
lexparser.bat texfile.txt
and the output would then be printed to the console.
I have shopped around and I have found some code which seems to be executing the file but I can't seem to extract the data being output, but I am unsure if this is correct.
QString pathDocument = qApp->applicationDirPath()+ "/stanford/lexparser.bat";
long result = (long)ShellExecute(0, 0, reinterpret_cast<const WCHAR*>(pathDocument.utf16()), 0, 0, SW_NORMAL);
I am using C++ as my language and I am also using the Qt Library to help me.
I have limited programming ability so any help would be greatly appreciated
I would recommend using QProcess for doing that.
See this question and its accepted answer for an example on how to do so.
I have a quick question, should be relatively simple for those who have some more experience in WMI-command processor than I do (and since I'm an absolute beginner thats not hard :-) )
I fail to understand why wmic /format switch works the way it does. I open up cmd.exe and type
wmic process list brief /format:htable > processlist.html
this does exactly what I want and no bothers further on. Whereas if I go to wmic processor, and try to execute the same command exactly as above...
wmic:root\cli>process list brief /format:htable > processlist.html
I receive the error tag: "Invalid XSL format (or) file name."
Here goes the screenshot. Note I have already copied XSL files from wbem to sys32 dir
Can someone explain to me why these 2 commands that for me look exactly the same, with the only difference that one is executed outside wmic environment and the other one is from inside, the latter one doesn't work? I just fail to understand it.
Please advise so I can comprehend this a bit better! :-)
Try this
copy /y %WINDIR%\system32\wbem\en-US\*.xsl %WINDIR%\system32\
And then
wmic:root\cli>process list brief /format:htable.xsl > processlist.html
Note the presence of the extension after "htable"
You are attempting to use CMD.EXE > redirection while you are within the interactive WMIC context. That can't work.
You can use the WMIC /output:filename switch while in interactive mode. Each subsequent command will overwrite the output of the previous command. You can get multiple commands to go to the same file by using /append:filename instead. You can reset the output back to stdout using /output:stdout.
/output:processlist.html
process list brief /format:htable
/output:stdout
Did you try specifying a full path in the wmic:root\cli>process call? My bets are that the first worked because it output the file to the current directory.
I am trying to execute a dos command from within my C++ program, however soon as I add quotes to the output filepath (of a redirection) the command no longer gets executed and returns instantly. I've shown an example below of a path without spaces, but since paths may have spaces and thus be quoted for the shell to understand it properly I need to solve this dilemma - and I'm trying to get the simplest case working first.
i.e.
The following WORKS:
sprintf(exec_cmd,"\"C:/MySQL Server 5.5/bin/mysqldump.exe\" -u%s -p%s %s > C:/backup.bak",user,password,db_name);
system(exec_cmd);
The following does NOT work (notice the quotes around the output):
sprintf(exec_cmd,"\"C:/MySQL Server 5.5/bin/mysqldump.exe\" -u%s -p%s %s > \"C:/backup.bak\"",user,password,db_name);
system(exec_cmd);
I'm guessing it is choking somewhere. I've tried the same "exec_cmd" in popen to no avail.
Any help/advice is greatly appreciated.
I don't think your shell (cmd.exe) allows redirection to a file name with spaces. I couldn't make my command.com from DOS 6.22 accept it (I don't have a cmd.exe nearby to test).
Anyway, you can use the --result-file option to pass the redirection to the command itself.
mysqldump ... --result-file="file name" ...