Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,584 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,952 people online right now. Registration is fast and FREE... Join Now!




Reading in a file in linux

 
Reply to this topicStart new topic

Reading in a file in linux

chaoticabyss99
26 Sep, 2008 - 11:35 AM
Post #1

D.I.C Head
**

Joined: 14 Jun, 2008
Posts: 92


My Contributions
I'm trying to write a small program that reads in files from the /proc directory. One of the files I have to read in is "/proc/uptime" to obtain Totaltime and Idletime. Then when all is said and done, I need to be able to display all info to the screen.

My question is: How do I read-in the files in the code?
User is offlineProfile CardPM
+Quote Post

girasquid
RE: Reading In A File In Linux
26 Sep, 2008 - 12:16 PM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,266



Thanked: 14 times
Dream Kudos: 650
My Contributions
CODE

string output = system('cat /proc/uptime');

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
User is offlineProfile CardPM
+Quote Post

chaoticabyss99
RE: Reading In A File In Linux
26 Sep, 2008 - 01:44 PM
Post #3

D.I.C Head
**

Joined: 14 Jun, 2008
Posts: 92


My Contributions
QUOTE(girasquid @ 26 Sep, 2008 - 01:16 PM) *

CODE

string output = system('cat /proc/uptime');

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.



OK, I know it's ugly, but here's what I've got.

CODE
#include <iostream>
#include <fstream>

using namespace std;

int main()
{

system('cat /proc/uptime');
system('cat /proc/sys/kernel/hostname');
system('cat /proc/version');
system('cat /proc/meminfo');
system('cat /proc/driver/rtc');

return 0;

}



I'm getting warning messages that say, "Character constant too long for its type."
And error messages that say, "Invalid conversion from 'int' to 'const char*'
initializing argument 1 of 'int system(const char*)' "

I know I'm missing some things but have no idea what they are. I just want to read in the info from these files and have that info displayed.

Please advise.

This post has been edited by chaoticabyss99: 26 Sep, 2008 - 01:47 PM
User is offlineProfile CardPM
+Quote Post

chaoticabyss99
RE: Reading In A File In Linux
26 Sep, 2008 - 09:42 PM
Post #4

D.I.C Head
**

Joined: 14 Jun, 2008
Posts: 92


My Contributions
I've been restlessly searching the web for tutorials and such that will help me fix this problem I have, but to no avail. Everything I have been finding is showing reading file I/O with a .txt file, or a .dat file. I can't find anything that would show how to do it with 'cat /proc/uptime' and whatnot.

Does anyone know where I can find some info that maybe has some examples of how to do file I/O in C++ for Linux? I would greatly appreciate it. Thanks.
User is offlineProfile CardPM
+Quote Post

realNoName
RE: Reading In A File In Linux
26 Sep, 2008 - 10:00 PM
Post #5

D.I.C Regular
***

Joined: 4 Dec, 2006
Posts: 299



Thanked: 5 times
My Contributions
replace all your single quotation mark with double quotation marks

you only use the single quote mark ' ' when you only have 1 character, when you have a string you need to use double quote marks " "
User is offlineProfile CardPM
+Quote Post

realNoName
RE: Reading In A File In Linux
26 Sep, 2008 - 10:10 PM
Post #6

D.I.C Regular
***

Joined: 4 Dec, 2006
Posts: 299



Thanked: 5 times
My Contributions
QUOTE(chaoticabyss99 @ 26 Sep, 2008 - 10:42 PM) *

I've been restlessly searching the web for tutorials and such that will help me fix this problem I have, but to no avail. Everything I have been finding is showing reading file I/O with a .txt file, or a .dat file. I can't find anything that would show how to do it with 'cat /proc/uptime' and whatnot.

Does anyone know where I can find some info that maybe has some examples of how to do file I/O in C++ for Linux? I would greatly appreciate it. Thanks.


as far as i know /proc/uptime is a program not a text file so what girasquid told you to do is call the system function to call the command "cat /proc/uptime" and that will print out the uptime and it will be saved to the output var

and as for doing i/o of files if you use the standard its the same as you would do in windows
http://www.cplusplus.com/doc/tutorial/files.html

This post has been edited by realNoName: 26 Sep, 2008 - 10:12 PM
User is offlineProfile CardPM
+Quote Post

chaoticabyss99
RE: Reading In A File In Linux
26 Sep, 2008 - 10:38 PM
Post #7

D.I.C Head
**

Joined: 14 Jun, 2008
Posts: 92


My Contributions
It figures it was something as simple as using double quotes instead of single quotes!

It works great and I got the result I was looking for. I went back and added some spacing and one liners to describe what was being displayed. Here it is:
CODE

#include <iostream>
#include <fstream>
using namespace std;

int main()
{

cout << "\nHere is the Totaltime and Idletime:\n" << endl;
system("cat /proc/uptime");
cout << "\nHere is the hostname:\n" << endl;
system("cat /proc/sys/kernel/hostname");
cout << "\nHere is the kernel version:\n" << endl;
system("cat /proc/version");
cout << "\nHere is the system memory info:\n" << endl;
system("cat /proc/meminfo");
cout << "\nHere is the time and date:\n" << endl;
system("cat /proc/driver/rtc");

return 0;

}


Thanks so much realNoName!! Thanks to girasquid also for getting me started!!

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:48AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month