473,411 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Open file fopen() and print values

23
How can i open a file with fopen and print the values?
Jul 4 '07 #1
48 4153
tnga
27
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
Jul 4 '07 #2
Wolkec
23
what pointer? And can you please post the code?
Jul 4 '07 #3
Silent1Mezzo
208 100+
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?
Jul 4 '07 #4
niskin
109 100+
You can open a file like this:

Expand|Select|Wrap|Line Numbers
  1. FILE *fp;
  2. 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
Jul 4 '07 #5
mohsin
19
You can open a file like this:

Expand|Select|Wrap|Line Numbers
  1. FILE *fp;
  2. 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+"
Jul 4 '07 #6
Silent1Mezzo
208 100+
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
Jul 4 '07 #7
Wolkec
23
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 :)
Jul 5 '07 #8
Wolkec
23
i also don't understand %f %s
:(
Jul 5 '07 #9
Meetee
931 Expert Mod 512MB
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
Jul 5 '07 #10
Wolkec
23
i know that u can save varible with &Var but i dont know what %s and %d is :S
Jul 5 '07 #11
Wolkec
23
oh, s & d are string and decimal integrer, but i dont know how to find Energy for User
Jul 5 '07 #12
Meetee
931 Expert Mod 512MB
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!! :))
Jul 5 '07 #13
Wolkec
23
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
Jul 5 '07 #14
Wolkec
23
oh, and i can edit the file
Jul 5 '07 #15
Meetee
931 Expert Mod 512MB
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
Jul 5 '07 #16
Wolkec
23
ok, ill try :):)
Any other way to search stuff in []
Jul 5 '07 #17
Meetee
931 Expert Mod 512MB
Let me be more clear.

Expand|Select|Wrap|Line Numbers
  1.  (from http://www.cplusplus.com/doc/tutorial/files.html)
  2. // reading a text file
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. using namespace std;
  7.  
  8. int main () {
  9.   string line;
  10.   ifstream myfile ("example.txt");
  11.   if (myfile.is_open())
  12.   {
  13.     while (! myfile.eof() )
  14.     {
  15.       getline (myfile,line);
  16.       /*here you can compare : line == "user" than take health and energy
  17.       and apply logic accordingly*/
  18.       cout << line << endl;
  19.     }
  20.     myfile.close();
  21.   }
  22.  
  23.   else cout << "Unable to open file"; 
  24.  
  25.   return 0;
  26. }
  27.  
Jul 5 '07 #18
Meetee
931 Expert Mod 512MB
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!
Jul 5 '07 #19
Wolkec
23
how do i take health and energy lol :S
Jul 5 '07 #20
Wolkec
23
and also, if i search for USER, that name might change later on, so how do i make string search for specific person
Jul 5 '07 #21
Meetee
931 Expert Mod 512MB
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;
}
Jul 5 '07 #22
Meetee
931 Expert Mod 512MB
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!!!
Jul 5 '07 #23
Wolkec
23
so, basicly, that extracts the text, should i use if to compare the string?
Jul 5 '07 #24
Wolkec
23
so, loop inside loop, or somethink else?
Jul 5 '07 #25
Meetee
931 Expert Mod 512MB
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
Jul 5 '07 #26
r035198x
13,262 8TB
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.
Jul 5 '07 #27
Wolkec
23
How do i display the values of the users :S
lol :)
Jul 5 '07 #28
Wolkec
23
1 string for each type (health, energy)?
Jul 5 '07 #29
Wolkec
23
and how do i dislpay the user inside []
Jul 5 '07 #30
r035198x
13,262 8TB
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.
Jul 5 '07 #31
Wolkec
23
lol, this is as far as i got :(
plz point me to the right direction:

Expand|Select|Wrap|Line Numbers
  1. int main ()
  2. {
  3.     string line;
  4.     ifstream fp("c:\\Vaja.ini");
  5.     if (fp.is_open())
  6.     {
  7.         while (! fp.eof())
  8.         {
  9.             getline (fp, line);
  10.                         if (line[0] == '[') {
  11.                 // this is player
  12.  
  13.             cout << line << endl;
  14.             }
  15.         }
  16.         fp.close();
  17.     }
  18.     else cout << "Vaja.ini doesn't exist or it cant be opened.\n";
  19.  
  20.         system("PAUSE");
  21.   return EXIT_SUCCESS;
  22. }
  23.  
and should i use string for each type (health, energy) or should i use loop:
Jul 5 '07 #32
niskin
109 100+
[quote=Wolkec]lol, this is as far as i got :(
plz point me to the right direction:

Expand|Select|Wrap|Line Numbers
  1. int main ()
  2. {
  3.     string line;
  4.                 //code
  5.     if (line[0] == '[') {
  6.                 //code
  7. }
  8.  
[quote]

You are treating line as if it is an array. Instead of:
Expand|Select|Wrap|Line Numbers
  1. string line;
Put:

Expand|Select|Wrap|Line Numbers
  1. char line[20]; //20 is just an example, you can have it any size you want
Jul 5 '07 #33
Wolkec
23
any other way? This one is hard :(
Jul 5 '07 #34
Wolkec
23
any other good way to search for user and then type his status?
Jul 5 '07 #35
Silent1Mezzo
208 100+
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.
Jul 5 '07 #36
tnga
27
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:

Expand|Select|Wrap|Line Numbers
  1. if (myarray[0]=="[") {
  2. // get the USER string here until you find "]"
  3. }
  4.  
Best regards,

TNGA
Jul 5 '07 #37
Wolkec
23
can any1 try and type the whole code :S i tried everything and it didnt work :(:(
Jul 5 '07 #38
tnga
27
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
Jul 5 '07 #39
Wolkec
23
I said i tried every think i know, and it doesnt work :S
Jul 5 '07 #40
tnga
27
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
Jul 5 '07 #41
niskin
109 100+
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:

Expand|Select|Wrap|Line Numbers
  1. #include <_stream> //note this underscore represents a missing letter as well!
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main ()
  7. {     
  8.     _ _ _ _ line[50];
  9.     _ _ _ _ _ _ _ _ fp("c:\\vaja.ini");
  10.     if (fp.is_open())
  11.     {     
  12.                      _ _>> line;
  13.                      _ _ _ _<< line << endl;
  14.                      system("PAUSE"); //OR cin.get(); (cin.get() is better programming unless you specifically want the user to press any key not just enter)
  15.     }
  16.  
  17.     if (!fp.is_open()) {
  18.          _ _ _ _ << "Vaja.ini doesn't exist or it cant be opened.\n";
  19.          system("PAUSE");
  20.          }
  21. }
Jul 5 '07 #42
tnga
27
Oh, dude, this is so cool!!!
That's why I love this site!
I think I know the answers! Lol
Jul 5 '07 #43
r035198x
13,262 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. #include <_stream> //note this underscore represents a missing letter as well!
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main ()
  7. {     
  8.     _ _ _ _ line[50];
  9.     _ _ _ _ _ _ _ _ fp("c:\\vaja.ini");
  10.     if (fp.is_open())
  11.     {     
  12.                      _ _>> line;
  13.                      _ _ _ _<< line << endl;
  14.                      system("PAUSE"); //OR cin.get(); (cin.get() is better programming unless you specifically want the user to press any key not just enter)
  15.     }
  16.  
  17.     if (!fp.is_open()) {
  18.          _ _ _ _ << "Vaja.ini doesn't exist or it cant be opened.\n";
  19.          system("PAUSE");
  20.          }
  21. }
Ha. Too many smart alecs around here
Jul 6 '07 #44
Silent1Mezzo
208 100+
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:

Expand|Select|Wrap|Line Numbers
  1. #include <_stream> //note this underscore represents a missing letter as well!
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main ()
  7. {     
  8.     _ _ _ _ line[50];
  9.     _ _ _ _ _ _ _ _ fp("c:\\vaja.ini");
  10.     if (fp.is_open())
  11.     {     
  12.                      _ _>> line;
  13.                      _ _ _ _<< line << endl;
  14.                      system("PAUSE"); //OR cin.get(); (cin.get() is better programming unless you specifically want the user to press any key not just enter)
  15.     }
  16.  
  17.     if (!fp.is_open()) {
  18.          _ _ _ _ << "Vaja.ini doesn't exist or it cant be opened.\n";
  19.          system("PAUSE");
  20.          }
  21. }
I'd like to buy a vowel.....'a'
Jul 6 '07 #45
niskin
109 100+
lol how did u know it was a? =O
Jul 9 '07 #46
tnga
27
lol how did u know it was a? =O
What about an "h" ? =O
Jul 10 '07 #47
niskin
109 100+
got it in 1, my friend
Jul 10 '07 #48
tnga
27
Hey, dude, I like the game....
But Wolvek never came back... =O
Jul 11 '07 #49

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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...
5
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...
10
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...
4
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...
9
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'...
1
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"...
7
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...
3
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...
3
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
0
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
0
BarryA
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...
1
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...
0
marktang
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,...
0
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.