How can i open a file with fopen and print the values?
48 4153
You should use fopen to open the file. It will return a file pointer that you may use to read the values (using fread) to your variables and then you may print the value of these variables, dude.
ps.: use fread if it's a binary file... if the file you want to read is text you may use fscanf, ok?
Best regards
TNGA
what pointer? And can you please post the code?
what pointer? And can you please post the code?
Heres how to do it in c++/c
We can't just give you the code though. How are you supposed to learn on your own?
You can open a file like this: - FILE *fp;
-
fp=fopen("example.txt", "r");
Then you can read the contents into variables using fscanf. Look up how to use fscanf because I don't want to give you all the source code as you won't learn that way. You need to look up file i/o as well. Here is a link to a useful site: www.cprogramming.com
You can open a file like this: - FILE *fp;
-
fp=fopen("example.txt", "r");
Then you can read the contents into variables using fscanf. Look up how to use fscanf because I don't want to give you all the source code as you won't learn that way. You need to look up file i/o as well. Here is a link to a useful site: www.cprogramming.com
yes u can do so in order to open a file
"r" for read
for writing you can use "w"
for reading and writing you can use "w+"
yes u can do so in order to open a file
"r" for read
for writing you can use "w"
for reading and writing you can use "w+"
You can also append b to the end of any of the modes to do it in binary
"rb" read binary
"wb" write binary
This is just another piece of information :P
the file im trying to open has:
[USER]
Energy=50
Health=30
[COMPUTER]
Energy=100
Health=10
Now how do i use fscanf to display "User has " << Energy << " energy and " << Health << " health.";
If you know what i mean :)
i also don't understand %f %s
:(
i also don't understand %f %s
:(
For that I suggest u to learn from the scratch... For now I can say %f indicates float value and %s indicates string :)
Regards
i know that u can save varible with &Var but i dont know what %s and %d is :S
oh, s & d are string and decimal integrer, but i dont know how to find Energy for User
i know that u can save varible with &Var but i dont know what %s and %d is :S
What u r talking about is scanf function..!
Any variable is of certain type...integer, float, string, char, boolean etc... for integer-%d, flaot-%f, string-%s, char-%c and so on...!!
like if u want to print any integer..u can write like this
int Wolk = 5;
printf("%d",Wolk);
Cheers!! :))
uh.. Can you tell me how to read from a file that says:
[USER]
Energy=50
Health=30
[COMPUTER]
Energy=100
Health=10
Now, how do i search values for USER and display its energy and health, and same for computer
oh, and i can edit the file
uh.. Can you tell me how to read from a file that says:
[USER]
Energy=50
Health=30
[COMPUTER]
Energy=100
Health=10
Now, how do i search values for USER and display its energy and health, and same for computer
That can be done. :))
Refer this link
You will get an idea of how to do this..!
Regards
ok, ill try :):)
Any other way to search stuff in []
Let me be more clear. - (from http://www.cplusplus.com/doc/tutorial/files.html)
-
// reading a text file
-
#include <iostream>
-
#include <fstream>
-
#include <string>
-
using namespace std;
-
-
int main () {
-
string line;
-
ifstream myfile ("example.txt");
-
if (myfile.is_open())
-
{
-
while (! myfile.eof() )
-
{
-
getline (myfile,line);
-
/*here you can compare : line == "user" than take health and energy
-
and apply logic accordingly*/
-
cout << line << endl;
-
}
-
myfile.close();
-
}
-
-
else cout << "Unable to open file";
-
-
return 0;
-
}
-
should i use fgetstring?
getline (myfile,line) would work for that! where line is string read from myfile.
Good luck :)
PS Search on google..u will get gr8 help!
how do i take health and energy lol :S
and also, if i search for USER, that name might change later on, so how do i make string search for specific person
and also, if i search for USER, that name might change later on, so how do i make string search for specific person
So you are making file having many users? Then also this loop will iterate until u get the person u want.
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
and also, if i search for USER, that name might change later on, so how do i make string search for specific person
So you are making file having many users? Then also this loop will iterate until u get the person u want.
while (! myfile.eof() )
{
getline (myfile,line);
COMPARISON
cout << line << endl;
}
Sorry for double posting!!!
so, basicly, that extracts the text, should i use if to compare the string?
so, loop inside loop, or somethink else?
so, basicly, that extracts the text, should i use if to compare the string?
Yes, better you start implementing the suggestions given uptil now and you will find the way!!
Regards
and also, if i search for USER, that name might change later on, so how do i make string search for specific person
You know you should really consider taking time to read a c++ tutorial on file handling. Getting the program to work might be cool but knowing how to get the program to work is even better.
How do i display the values of the users :S
lol :)
1 string for each type (health, energy)?
and how do i dislpay the user inside []
and how do i dislpay the user inside []
I suggest you read my reply #27 above and follow the advice there.
This thread may soon be closed if you won't take the advice.
lol, this is as far as i got :(
plz point me to the right direction: - int main ()
-
{
-
string line;
-
ifstream fp("c:\\Vaja.ini");
-
if (fp.is_open())
-
{
-
while (! fp.eof())
-
{
-
getline (fp, line);
-
if (line[0] == '[') {
-
// this is player
-
-
cout << line << endl;
-
}
-
}
-
fp.close();
-
}
-
else cout << "Vaja.ini doesn't exist or it cant be opened.\n";
-
-
system("PAUSE");
-
return EXIT_SUCCESS;
-
}
-
and should i use string for each type (health, energy) or should i use loop:
[quote=Wolkec]lol, this is as far as i got :(
plz point me to the right direction: - int main ()
-
{
-
string line;
-
//code
-
if (line[0] == '[') {
-
//code
-
}
-
[quote]
You are treating line as if it is an array. Instead of:
Put: - char line[20]; //20 is just an example, you can have it any size you want
any other way? This one is hard :(
any other good way to search for user and then type his status?
any other way? This one is hard :(
Programmings not always easy :P Sometimes the best way to do something is the hardest. I would advise you first read a c++ tutorial because us just giving you the answers really isn't helping you.
any other way? This one is hard :(
Well, it's not that hard!
What you need to know is:
1. How to treat text files
Some people has already pointed you the right way. Read info on how to open a file using fopen and how to read strings using fscanf with %s into a char array.
2. How to read values from a char array
For example, if you want to get "USER" you should make somthing like this: -
if (myarray[0]=="[") {
-
// get the USER string here until you find "]"
-
}
-
Best regards,
TNGA
can any1 try and type the whole code :S i tried everything and it didnt work :(:(
Dude, I think we can't do it... It goes against the forum rules.
We all want to help you, but the right way for you to learn is...learning
and not taking the full code!
Try to follow my tips, search for tutorials on how to treat files and char arrays.
Best regards,
TNGA
I said i tried every think i know, and it doesnt work :S
I said i tried every think i know, and it doesnt work :S
Yes, you'd better try to learn something new. How about reading some tutorials?
I gave you the road to track after you said it was too difficult. After that you said you've tried everything but it hasn't passed enough time for a normal being to search stuff and read.
So, I keep my advice on trying to search and learn that stuff.
Best regards,
TNGA
I've never seen this done before on thescripts, but I'm going to give you some code and you have to fill in the gaps. Each underscore represents 1 letter you need to fill in. So a series of four underscores means you need to type a four letter word: - #include <_stream> //note this underscore represents a missing letter as well!
-
#include <iostream>
-
-
using namespace std;
-
-
int main ()
-
{
-
_ _ _ _ line[50];
-
_ _ _ _ _ _ _ _ fp("c:\\vaja.ini");
-
if (fp.is_open())
-
{
-
_ _>> line;
-
_ _ _ _<< line << endl;
-
system("PAUSE"); //OR cin.get(); (cin.get() is better programming unless you specifically want the user to press any key not just enter)
-
}
-
-
if (!fp.is_open()) {
-
_ _ _ _ << "Vaja.ini doesn't exist or it cant be opened.\n";
-
system("PAUSE");
-
}
-
}
Oh, dude, this is so cool!!!
That's why I love this site!
I think I know the answers! Lol
I've never seen this done before on thescripts, but I'm going to give you some code and you have to fill in the gaps. Each underscore represents 1 letter you need to fill in. So a series of four underscores means you need to type a four letter word: - #include <_stream> //note this underscore represents a missing letter as well!
-
#include <iostream>
-
-
using namespace std;
-
-
int main ()
-
{
-
_ _ _ _ line[50];
-
_ _ _ _ _ _ _ _ fp("c:\\vaja.ini");
-
if (fp.is_open())
-
{
-
_ _>> line;
-
_ _ _ _<< line << endl;
-
system("PAUSE"); //OR cin.get(); (cin.get() is better programming unless you specifically want the user to press any key not just enter)
-
}
-
-
if (!fp.is_open()) {
-
_ _ _ _ << "Vaja.ini doesn't exist or it cant be opened.\n";
-
system("PAUSE");
-
}
-
}
Ha. Too many smart alecs around here
I've never seen this done before on thescripts, but I'm going to give you some code and you have to fill in the gaps. Each underscore represents 1 letter you need to fill in. So a series of four underscores means you need to type a four letter word: - #include <_stream> //note this underscore represents a missing letter as well!
-
#include <iostream>
-
-
using namespace std;
-
-
int main ()
-
{
-
_ _ _ _ line[50];
-
_ _ _ _ _ _ _ _ fp("c:\\vaja.ini");
-
if (fp.is_open())
-
{
-
_ _>> line;
-
_ _ _ _<< line << endl;
-
system("PAUSE"); //OR cin.get(); (cin.get() is better programming unless you specifically want the user to press any key not just enter)
-
}
-
-
if (!fp.is_open()) {
-
_ _ _ _ << "Vaja.ini doesn't exist or it cant be opened.\n";
-
system("PAUSE");
-
}
-
}
I'd like to buy a vowel.....'a'
lol how did u know it was a? =O
lol how did u know it was a? =O
What about an "h" ? =O
Hey, dude, I like the game....
But Wolvek never came back... =O
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Paul C-T |
last post by:
Hi,
Am I trying to be too clever here?
I am trying to write a PHP page to enable me to enter values into a form
then write those values to a text file.
I want to use the form & table that...
|
by: Saket |
last post by:
Hello All,
I am want to open a file in C language where in i know the location of
the file, but do not know its name.
For this what i have done is: -
1) Used system command like and listed...
|
by: Grocery Clerk |
last post by:
I know open() returns a file descriptor and fopen() returns a pointer
to FILE. The question is, when do I use fopen() and when do I use
open()? Could someone give me an example when to use one...
|
by: Frank |
last post by:
Could someone tell me how to open a file at run time
that I didn't know the name of at compile time?
I know how to open a file at compile time when I know
what the name is going to be.
FILE...
|
by: ferbar |
last post by:
Hi all,
I'm trying to read from the txt file 'ip.packets.2.txt' using the read
function. It seems everything ok, but I get a -1 when executing
>>bytesr = read(fdo1, bufread, 2);
The 'open'...
|
by: laredotornado |
last post by:
Hi,
I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed
using Apache's apxs and the php library was installed to
/usr/local/php. However, when I set my "error_reporting"...
|
by: Efi Merdler |
last post by:
Hi,
I created a very simple application that reads and writes to a file, I
was wondering if fopen when opening a file for write access blocks
this file, i.e. when another user runs this script the...
|
by: siyaverma |
last post by:
i am trying to upload csv file from user's computer to main server
the code i am using is
if(((isset($_GET)) && ($_GET=="yes")) )
{
$typefield = $_GET;
echo...
|
by: magicman |
last post by:
Is difference lies in the fact that fopen part of c library and
platform in-depended, whereas open is a system call? what about
functionalities?
thx
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |