Opening and outputting a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Transmit
    New Member
    • Dec 2006
    • 11

    Opening and outputting a text file

    Hi everyone,

    I want to open a .txt file and just show it.Not for reading or anything but just showing it when people enter a certain number.
    Can anyone tell me how to do this?

    and is there a command to close all windows of the application?

    IM DOING C++ IN BORLAND
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by Transmit
    Hi everyone,

    I want to open a .txt file and just show it.Not for reading or anything but just showing it when people enter a certain number.
    Can anyone tell me how to do this?

    and is there a command to close all windows of the application?

    IM DOING C++ IN BORLAND
    Transmit -

    This will help you open a file, read stuff in, and then you can print it out to the screen:



    You could use a case (switch) statement in a while loop for the user input and when to display, if you need help on that, check out thescripts.com' s tutorial:



    That should get you started!

    Comment

    • Transmit
      New Member
      • Dec 2006
      • 11

      #3
      Thanks, but I actually meant opening the .txt file like when you would double click on it.
      Sorry if I didn't explain well!

      and do you know how to close the whole program? I tried abort() but that didn't gave me what I wanted!

      thx already!!

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by Transmit
        Thanks, but I actually meant opening the .txt file like when you would double click on it.
        Sorry if I didn't explain well!

        and do you know how to close the whole program? I tried abort() but that didn't gave me what I wanted!

        thx already!!
        I would guess you are using Windows, and I'm not entirely sure how to open it like that - as it involves a system command to open Word/TextPad/Notepad to open the file up... You can always just open it yourself and print it on the screen (unless you want someone to be able to edit it...). But that shouldn't be too hard to google and find out - I would guess it uses 'windows.h' to run whatever the executable is, and possibly send the file as a parameter? I'm not a windows guy, so I'm not too sure... (sorry!)

        You can end the program through return 1 or return 0;

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          I know how to open it - just pass the system() function a string that has the path to the file you want to open, i.e.

          Code:
          system("C:\\My Documents\\My Folder\\MyFile.txt"); // Double slashes needed because \\ is escape character for \
          The system() function takes a string and performs operations just like your regular MSDOS-style command prompt would with the same string. For instance, if I wanted to display the files listed in the folder with my program, I could use the command

          Code:
          system("DIR");
          because dir is the command I use in MSDOS to do the same thing.

          Closing the file, though...I don't know.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by Ganon11
            I know how to open it - just pass the system() function a string that has the path to the file you want to open, i.e.

            Code:
            system("C:\\My Documents\\My Folder\\MyFile.txt"); // Double slashes needed because \\ is escape character for \
            The system() function takes a string and performs operations just like your regular MSDOS-style command prompt would with the same string. For instance, if I wanted to display the files listed in the folder with my program, I could use the command

            Code:
            system("DIR");
            because dir is the command I use in MSDOS to do the same thing.

            Closing the file, though...I don't know.

            Thanks, Ganon11 - I knew there was a way to do it, but wasn't entirely sure how. And I'm pretty sure whatever user who this file is displayed to is going to have to be in charge of closing it.

            Comment

            • Transmit
              New Member
              • Dec 2006
              • 11

              #7
              I put this:
              system("C:\\Sch ool\\Gestructur eerd programmeren\\P rogrammeerproje ct\\Handleiding C.txt");

              and it gave me this error:
              linking error: undefined symbol _system in module simon.C

              any thoughts?

              Comment

              • Transmit
                New Member
                • Dec 2006
                • 11

                #8
                sorry for double post but the 5 minutes expired:

                I put the code I just put above ^^ in a switch case statement

                so it would look like this:

                switch(keuze){
                case 1:
                system("pathto. txtfile");
                break;
                case 2:
                ...
                }

                I guess that's totally wrong :D Can you specify how to do it a bit more? Thanks man you're being a great help!!!

                appreciate it!

                Comment

                • sicarie
                  Recognized Expert Specialist
                  • Nov 2006
                  • 4677

                  #9
                  Originally posted by Transmit
                  sorry for double post but the 5 minutes expired:

                  I put the code I just put above ^^ in a switch case statement

                  so it would look like this:

                  switch(keuze){
                  case 1:
                  system("pathto. txtfile");
                  break;
                  case 2:
                  ...
                  }

                  I guess that's totally wrong :D Can you specify how to do it a bit more? Thanks man you're being a great help!!!

                  appreciate it!
                  Can you post the entire program?

                  Comment

                  • willakawill
                    Top Contributor
                    • Oct 2006
                    • 1646

                    #10
                    Originally posted by Transmit
                    sorry for double post but the 5 minutes expired:

                    I put the code I just put above ^^ in a switch case statement

                    so it would look like this:

                    switch(keuze){
                    case 1:
                    system("pathto. txtfile");
                    break;
                    case 2:
                    ...
                    }

                    I guess that's totally wrong :D Can you specify how to do it a bit more? Thanks man you're being a great help!!!

                    appreciate it!
                    do you have this at the top of your file;
                    Code:
                    #include "stdlib.h"

                    Comment

                    • Transmit
                      New Member
                      • Dec 2006
                      • 11

                      #11
                      well the program is kinda big and yes I typed:

                      #include <stdlib.h>

                      on top!

                      Comment

                      • sicarie
                        Recognized Expert Specialist
                        • Nov 2006
                        • 4677

                        #12
                        Originally posted by Transmit
                        well the program is kinda big and yes I typed:

                        #include <stdlib.h>

                        on top!
                        How big is "kinda big"? We need more to be able to figure this out. If I wrote what I thought was in your code before/after/in the switch statement, I'm pretty sure it would be a completely different program. The entire error message would help too.

                        Comment

                        • Transmit
                          New Member
                          • Dec 2006
                          • 11

                          #13
                          Here you are then:

                          Code:
                          #include <stdio.h>
                          #include <stdlib.h> /*nodig voor random()*/
                          #include <time.h> /*gaat ermee voor zorgen dat niet telkens dezelfde getallen worden opgeroepen*/
                          #include <windows.h>
                          
                          int welkomtekst(void);
                          int keuze1(void);
                          int startspel(void);
                          int moeilijkheidsgraad(void);
                          int highscores(void);
                          int eerstekeeropgestart = 0;
                          int aantalgetallen = 1;
                          int highscore = 0;
                          int moeilijkheid = 1;
                          
                          int main(void){
                          	int keuze;
                          	int adminnaam;
                          	int adminpaswoord;
                          	int system(const char *handleiding);
                          
                          	fflush(stdin);
                          	aantalgetallen = 1;
                          	highscore = 0;
                          	clrscr();
                          
                          	if(eerstekeeropgestart != 1){
                          		printf("Omdat dit de eerste keer is dat SIMON wordt opgestart,\nmoet er een administrator naam en paswoord ingegeven worden\n\n\n");
                          		printf("Geef de administrator naam in: ");
                          		scanf("%d",&adminnaam);
                          		printf("Geef het administrator paswoord in: ");
                          		scanf("%d",&adminpaswoord);
                          
                          		eerstekeeropgestart = 1;
                          		main();
                          	}
                          
                          	printf("Welkom bij Simon!\n\n");
                          	welkomtekst();
                          	scanf("%d",&keuze);
                          	switch(keuze){
                          		case 1 :
                          			clrscr();
                          			keuze1();
                          			break;
                          		case 2:
                          			moeilijkheidsgraad();
                          			break;
                          		case 3:
                          			break;
                          		case 4:
                          			break;
                          		case 5:
                          			highscores();
                          			break;
                          		case 6:
                          			break;
                          		case 7:
                          			abort();
                          			break;
                          		default:
                          			printf("Foutieve ingave");
                          			main();
                          			break;
                          	}
                          	return 0;
                          }
                          
                          int welkomtekst(void){
                          	clrscr();
                          
                          	printf("|-----------------------------------\n");
                          	printf("|Spel gemaakt door Vandersteegen Jan\n");
                          	printf("|-----------------------------------\n\n");
                          
                          	printf("|------------------------------------------\n");
                          	printf("| [][][]  °°  [][][][]  [][][]  [][]    [] \n");
                          	printf("| []      []  [] [] []  []  []  [] []   [] \n");
                          	printf("| [][][]  []  []    []  []  []  []   [] [] \n");
                          	printf("|     []  []  []    []  []  []  []    |][] \n");
                          	printf("| [][][]  []  []    []  [][][]  []      [] \n");
                          	printf("|------------------------------------------\n\n");
                          	printf("Maak een keuze uit de volgende mogelijkheden: \n\n");
                          
                          	printf("******************************\n");
                          	printf("1: Nieuw spel \n");
                          	printf("2: Moelijkheidsgraad kiezen \n");
                          	printf("3: Herhaling \n");
                          	printf("4: Handleiding \n");
                          	printf("5: Highscores inkijken \n");
                          	printf("6: Inloggen als administrator\n");
                          	printf("7: Programma afsluiten \n");
                          	printf("******************************\n\n");
                          
                          	printf("Uw keuze: ");
                          	return 0;
                          }
                          
                          void sleep(unsigned int mseconds){ /*Wordt gebruikt om te wachten bij het genereren van nieuwe getallen*/
                          	 clock_t goal = mseconds + clock();
                          	 while (goal > clock());
                          }
                          
                          int highscores(void){
                          	 char gedrukt;
                          	 clrscr();
                          	 fflush(stdin);
                          	 printf("Druk op 'X' om terug naar het hoofdmenu te gaan: ");
                          	 scanf("%c",&gedrukt);
                          	 if ((gedrukt == 'x')||(gedrukt =='X')){
                          		 main();
                          	 }else{
                          		 highscores();
                          	 }
                          }
                          int handleiding(void){
                          
                          }
                          int moeilijkheidsgraad(void){
                          	 clrscr();
                          	 fflush(stdin);
                          	 printf("INFO: Kijk in de handleiding voor de verschillen tussen de moeilijkheidsgraden\n\n");
                          	 printf("Kies hier de moeilijkheidsgraad: \n\n");
                          	 printf("1: Makkelijk\n");
                          	 printf("2: Gewoon\n");
                          	 printf("3: Moeilijk\n");
                          	 printf("4: Superman stijl\n\n");
                          	 printf("Uw keuze: ");
                          	 scanf("%d",&moeilijkheid);
                          	 printf("Keuze geregistreerd!\nU wordt binnen enkele seconden terug verwezen naar het hoofdmenu!\n");
                          	 sleep(2500);
                          	 fflush(stdin);
                          	 main();
                          }
                          int keuze1(void){
                          	 char enter;
                          
                          	 fflush(stdin);
                          
                          	 printf("*** LET THE GAMES BEGIN ***\n\n");
                          	 printf("U kan terug naar het hoofdmenu gaan door op 'X' te duwen\n\n");
                          	 printf("Druk op ENTER als u klaar bent om te starten\n\n");
                          	 scanf("%c",&enter);
                          	 if(enter == '\n'){
                          	  printf("HOU U KLAAR WANT BINNEN 2 SECONDEN START HET SPEL !!!");
                          	  sleep(2000);
                          	  startspel();
                          	 }else{
                          		if ((enter == 'x')||(enter =='X')){
                          		 main();
                          		}
                          		printf("\nFoutieve invoer! Druk op ENTER of op X\n\n");
                          		keuze1();
                          	 }
                          	 return 0;
                          }
                          
                          int startspel(void){
                          	 int t = 0;
                          	 int tabel1[25] = {0};
                          	 int tabel2[25] = {0};
                          	 int randomgetal;
                          	 int keuze;
                          	 int aantalgoed = 0;
                          
                          	 aantalgetallen++;
                          
                          	 srand(time(NULL));   /*zorgt ervoor dat niet altijd dezelfde random getallen gegenereert worden*/
                          	 sleep(500);
                          	 for(t=0;t<=aantalgetallen;t++){
                          		clrscr();
                          		randomgetal = rand()%5;
                          		tabel1[t] = randomgetal;
                          		printf("Druk op X om terug te gaan naar het hoofdmenu\n\n");
                          		printf("%de na te typen getal: ",t+1);
                          		printf("%i \n",randomgetal);
                          		tabel1[t] = randomgetal;
                          		switch(moeilijkheid){
                          			case 1:
                          				sleep(1000);
                          			case 2:
                          				sleep(750);
                          			case 3:
                          				sleep(500);
                          			case 4:
                          				sleep(200);
                          		}
                          	 }
                          	 for(t=0;t<=aantalgetallen;t++){
                          		clrscr();
                          		printf("Druk op X om terug te gaan naar het hoofdmenu\n\n");
                          		printf("*-*-* Uw score is: %d *-*-* \n\n",highscore);
                          		printf("Typ het %de getoonde getal: ",t+1);
                          		scanf("%d",&tabel2[t]);
                          		if(tabel2[t] != tabel1[t]){
                          			fflush(stdin);
                          			printf("\n FOUT - GAME OVER!\n\n");
                          			printf("Wat wilt u doen?\n");
                          			printf("1: Een nieuw spel starten\n");
                          			printf("2: Terug naar het hoofdmenu\n");
                          			printf("3: Highscores bekijken\n");
                          			printf("4: Moeilijkheidsgraad veranderen\n");
                          			printf("Uw keuze: ");
                          			scanf("%d",&keuze);
                          			switch(keuze){
                          				case 1 :
                          					clrscr();
                          					aantalgetallen = 1;
                          					highscore = 0;
                          					keuze1();
                          					break;
                          				case 2:
                          					main();
                          					break;
                          				case 3:
                          					printf("Nog niet klaar");
                          					break;
                          				case 4:
                          					moeilijkheidsgraad();
                          					break;
                          				default:
                          					printf("Foutieve invoer: U wordt binnen een paar seconden naar het hoofdmenu gebracht");
                          					sleep(1500);
                          					main();
                          			}
                          		}else{
                          			highscore+=100;
                          			aantalgoed++;
                          			if (aantalgoed == (aantalgetallen + 1)){
                          				startspel();
                          			}
                          		}
                          	 }
                          }

                          Comment

                          Working...