| Author |
Message |
Cerulean Gokenin
Joined: 22 Oct 2004 Posts: 742 Location: London, England
|
Posted: Fri Feb 20, 2004 7:46 am Post subject: Re: Opening a text file and viewing it's contents |
|
|
Linux (Bash Shell)
head -100000 file.txt
Revenge is a dish best served cold
pyre.tk: Programming, Scripting, wallpapers, tutorials.
Programmers corner: source code, helpful people.VB, .net, c/c++, java, html/side scriptsEdited by: blu fire at: 2/21/04 4:15 pm
|
|
| Back to top |
|
 |
Qnarcus@ Ronin
Joined: 19 Feb 2004 Posts: 21
|
Posted: Sun Feb 22, 2004 11:35 am Post subject: Re: Opening a text file and viewing it's contents |
|
|
C++
#include <iostream> #include <fstream>using namespace std;int main( int argc, char* argv[]){ ifstream file (argv[1]); if (!file.is_open()) { cout<<"Error opening file "<<argv[]; return 0; } char buffer; while (!file.eof()) { file.get (buffer); cout<< buffer; } file.close();}
If .exe file is named "file.exe", then it call it from commandline>> read textfile.txt
A-hah! Get it! Not HTML, ezCode! :) Thanks eric Edited by: Qnarcus at: 3/7/04 10:09 pm
|
|
| Back to top |
|
 |
Cerulean Gokenin
Joined: 22 Oct 2004 Posts: 742 Location: London, England
|
Posted: Sun Mar 07, 2004 3:56 pm Post subject: Re: Opening a text file and viewing it's contents |
|
|
naughty naughty, you forgot to include iostream. That won't compile...... : )
People say life is long... Whats longer??
pyre.tk: Programming, Scripting, wallpapers, tutorials.
Programmers corner: source code, helpful people.VB, .net, c/c++, java, html/side scriptsEdited by: blu fire at: 3/7/04 3:56 pm
|
|
| Back to top |
|
 |
Qnarcus@ Ronin
Joined: 19 Feb 2004 Posts: 21
|
Posted: Sun Mar 07, 2004 10:09 pm Post subject: Re: Opening a text file and viewing it's contents |
|
|
heh, you got me :) Guess I never compiled it... *edited*
|
|
| Back to top |
|
 |
katy1062@ Grasshopper
Joined: 13 Apr 2004 Posts: 2
|
Posted: Tue Apr 13, 2004 8:44 am Post subject: Re: Opening a text file and viewing it's contents |
|
|
perl
#!perl # open(INPUT, "filename"); print <INPUT>; |
|
| Back to top |
|
 |
Cerulean Gokenin
Joined: 22 Oct 2004 Posts: 742 Location: London, England
|
Posted: Wed Apr 14, 2004 5:35 am Post subject: Re: Opening a text file and viewing it's contents |
|
|
Welcome to programmers corner! PyreSoft: Programming, WebPage templates, custome webpage design, programs, etc.. Programmers corner: source code, helpful people. .net, c/c++, java, html/side scripts |
|
| Back to top |
|
 |
TrinityTime@ Grasshopper
Joined: 14 Apr 2004 Posts: 18
|
Posted: Fri Apr 30, 2004 10:41 am Post subject: Re: Opening a text file and viewing it's contents |
|
|
C++ This is how you save data to a .txt file Don't mind the names of the variables. I took that code straight from a game that I made.
//This program saves data into a new .txt file #include <iostream.h> #include <stdio.h> #include <conio.h> #include <fstream.h>
void main() { int health = 50; int magic = 75; int location = 1; int Items = 1000;
ofstream f("C:\\savegame.txt"); f<<health<<" "<<magic<<" "<<location<<" "; f<<Items<<" "; cout << "The program has saved a .txt file to the directory: C:\\savegame.txt\n"; cout << "Press any key to continue........"; getch(); }
This is how you read data from a .txt file
//This is a program that reads the data from //that .txt document you just saved........ #include <iostream.h> #include <stdio.h> #include <conio.h> #include <fstream.h>
void main() { int health3; int magic3; int location3; int Items3; ifstream f("C:\\savegame.txt"); f>>health3>>magic3>>location3>>Items3; cout << health3 << " "<<magic3 << " "<<location3<<" "<<Items3; cout << "\nThose are the values that was stored in the .txt document.\n"; cout << "Press any key to continue........"; getch(); }
P.S. I got that from my new forum that I made. Check it out. ##PROGRAMMING LINKS## #My Old Forum #My New Forum #My Friends VB Yahoo Group Edited by: TrinityTime at: 4/30/04 11:41 am
|
|
| Back to top |
|
 |
Algorithms Dragon
Joined: 21 Oct 2004 Posts: 343 Location: Florida
|
Posted: Wed Jul 07, 2004 2:06 am Post subject: Re: Opening a text file and viewing it's contents |
|
|
In Python
| Code: |
fp = open("filename", "r")
done = 0
while not done:
line = fp.readline()
if line != "":
print line
else:
done = 1
fp.close()
|
OR maybe
| Code: |
fp = open("filename", "r")
al = fp.readlines()
fp.close()
for l in al:
print l
|
Depending on my needs.
Last edited by Algorithms on Thu Feb 24, 2005 2:45 pm; edited 1 time in total |
|
| Back to top |
|
 |
corbaone Guest
|
Posted: Fri Feb 11, 2005 2:59 pm Post subject: in java |
|
|
| Code: |
import java.io.*;
public class Foo {
public static void main(String[] args) {
try{
BufferedReader read = new BufferedReader(new FileReader("foo.txt"));
while(read.ready()) {
System.out.println(read.readLine());
}
} catch(IOException e) {
System.err.println(e.getMessage());
}
}
}
|
|
|
| Back to top |
|
 |
Eric Idea Hamster
Joined: 21 Oct 2004 Posts: 679 Location: Bangor, Maine
|
Posted: Fri Feb 11, 2005 8:59 pm Post subject: |
|
|
DOS:
type CodeWar.txt > MyFile.txt
Or, to dump to screen:
type CodeWar.txt |
|
| Back to top |
|
 |
|