Programmer's Corner Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Programmer's Corner - Forums


Opening a text file and viewing its contents
Goto page Previous  1, 2
 
Post new topic   Reply to topic    Programmer's Corner Forum Index -> Code Wars
Author Message
Cerulean
Gokenin


Joined: 22 Oct 2004
Posts: 742
Location: London, England

PostPosted: Fri Feb 20, 2004 7:46 am    Post subject: Re: Opening a text file and viewing it's contents Reply with quote

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 scripts

Edited by: blu fire at: 2/21/04 4:15 pm
Back to top
Qnarcus@
Ronin


Joined: 19 Feb 2004
Posts: 21

PostPosted: Sun Feb 22, 2004 11:35 am    Post subject: Re: Opening a text file and viewing it's contents Reply with quote

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

PostPosted: Sun Mar 07, 2004 3:56 pm    Post subject: Re: Opening a text file and viewing it's contents Reply with quote

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 scripts

Edited by: blu fire at: 3/7/04 3:56 pm
Back to top
Qnarcus@
Ronin


Joined: 19 Feb 2004
Posts: 21

PostPosted: Sun Mar 07, 2004 10:09 pm    Post subject: Re: Opening a text file and viewing it's contents Reply with quote

heh, you got me :) Guess I never compiled it...
*edited*


Back to top
katy1062@
Grasshopper


Joined: 13 Apr 2004
Posts: 2

PostPosted: Tue Apr 13, 2004 8:44 am    Post subject: Re: Opening a text file and viewing it's contents Reply with quote

perl

#!perl
#
open(INPUT, "filename");
print <INPUT>;

Back to top
Cerulean
Gokenin


Joined: 22 Oct 2004
Posts: 742
Location: London, England

PostPosted: Wed Apr 14, 2004 5:35 am    Post subject: Re: Opening a text file and viewing it's contents Reply with quote

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

PostPosted: Fri Apr 30, 2004 10:41 am    Post subject: Re: Opening a text file and viewing it's contents Reply with quote

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

PostPosted: Wed Jul 07, 2004 2:06 am    Post subject: Re: Opening a text file and viewing it's contents Reply with quote

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





PostPosted: Fri Feb 11, 2005 2:59 pm    Post subject: in java Reply with quote

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

PostPosted: Fri Feb 11, 2005 8:59 pm    Post subject: Reply with quote

DOS:

type CodeWar.txt > MyFile.txt

Or, to dump to screen:

type CodeWar.txt
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Programmer's Corner Forum Index -> Code Wars All times are GMT - 5 Hours
Goto page Previous  1, 2
Page 2 of 2

 


Powered by phpBB © 2001, 2002 phpBB Group