9 Replies - 2195 Views - Last Post: 02 June 2007 - 04:02 PM Rate Topic: -----

#1 dragonsword1  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 02-February 07

how to make a text file

Post icon  Posted 31 May 2007 - 04:37 PM

well i knew how but i forgot and i couldnt get it from my reource and if u dont mind to help me then please do




thanks in advance
Is This A Good Question/Topic? 0
  • +

Replies To: how to make a text file

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: how to make a text file

Posted 31 May 2007 - 05:31 PM

View Postdragonsword1, on 31 May, 2007 - 04:37 PM, said:

well i knew how but i forgot and i couldnt get it from my reource and if u dont mind to help me then please do

thanks in advance


fopen(file,"w");




http://www.cplusplus...tdio/fopen.html


"w Creates an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file."
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: how to make a text file

Posted 31 May 2007 - 08:29 PM

There are two methods. One works only in C++ using streams, and the other uses the older C interface (which still works in C++).

To create a text file in C
first create a file handle/pointer
FILE *fp;
next open the the file
fp = fopen(const char * FileName, const char *AccessType);

the AcessTypes are:
"r" - read only
"w" - create a new file for writing to...
"a" - Append opens a file, sets the cursor to the end of the file...
"r+" - opens an existing file for reading and writing
"w+" - creates a new file for reading and writing
"a+" Opens an existing file for reading and writing, sets the cursor to the end of the file.

You can then write to the file using fputc(), fputs(), and fprintf().
You can read using fgetc(), fgets(), and fscanf().

To open files using C++'s streams you can define an ifstream (input streams read files) or an ofstream (output streams write to files).

once you have opened the file you can use the "<<" and ">>" operators much as you do with cout and cin to read and write to the files.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: how to make a text file

Posted 31 May 2007 - 08:39 PM

That was a much better, & complete post! Thank you NickDMax.
Was This Post Helpful? 0
  • +
  • -

#5 dragonsword1  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 02-February 07

Re: how to make a text file

Posted 01 June 2007 - 12:42 AM

thanksssss very much
Was This Post Helpful? 0
  • +
  • -

#6 dragonsword1  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 02-February 07

Re: how to make a text file

Posted 01 June 2007 - 12:51 AM

WELL I MADE A CODE THAT MAKE A TEXT FILE AND IN IT THE NUMBER AND THEN THE CHAR LIKE THAT
1 ☺


AND HERE IS MY CODE

#include <iostream.h>

#include <stdlib.h>
#include <stdio.h>

 

int main()

{
		 for (int i = 1; i<128; i++)

		 //display up to 127...
		  FILE * pFile;
		  pFile = fopen ("myfile.txt","w");
		  if (pFile!=NULL)
		  {
		  fputs (i<<"	"<<(char) i<<"\n",pFile);
		  fclose (pFile);
		  }


		 //simple typecasting, from int to char

		 system("pause");

		 return 0;

}



This post has been edited by dragonsword1: 01 June 2007 - 12:55 AM

Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: how to make a text file

Posted 01 June 2007 - 02:05 AM

lol, well I tried to show that there were two methods, but I guess I forgot to mention that you should not MIX them!!!.

This line fputs (i<<" "<<(char) i<<"\n",pFile); is ... well lets just say that it does not work. So lets see if you can shift things about here...

#include <iostream>
#include <cstdlib>
#include <cstdio>

int main()
{
	//We really want to open the file before the loop...
	//If we open the file inside the loop we will only write 1 number.
	//Since the "w" option will create a new file EACH TIME IT RUNS!
	FILE * pFile;
	pFile = fopen ("myfile.txt","w"); //Create a file name "myfile.txt" in the current directory.
	if (pFile!=NULL)
	{
		for (int i = 1; i<128; i++) //Need to use { } to make this work...
		{
			//The fprintf() function works just like the printf() one does...
			fprintf(pFile, "%d is Char: %c\n", i, (char) i); //Write data to the the file...
		}
	}
	fclose (pFile);
	system("pause");
	return 0;
}



OR, you could have gone another way and use the C++ streams:
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	ofstream oFile;
	oFile.open("myFile.txt");
	if (oFile)
	{
		for (int i = 1; i<128; i++) //Need to use { } to make this work...
		{
			//the oFile object will work just like cout did...
			oFile << i << " is Char: " << (char) i << endl;
		}
	}
	oFile.close();
	system("pause");
	return 0;
}



OF course the above two programs do have a little problem to them. You start with char 1 and go to 128... but the first 32 ascii characters are control characters and many are not valid inside of a text file... So to really make the programs work so that you don't get strange characters you need to start your loop at 32.
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: how to make a text file

Posted 01 June 2007 - 05:41 AM

View PostNickDMax, on 1 Jun, 2007 - 02:05 AM, said:

OF course the above two programs do have a little problem to them. You start with char 1 and go to 128... but the first 32 ascii characters are control characters and many are not valid inside of a text file... So to really make the programs work so that you don't get strange characters you need to start your loop at 32.


Or you could test against [a-zA-Z0-9] sort of thing...
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: how to make a text file

Posted 01 June 2007 - 06:00 AM

Well if the idea is to discover what the characters are for the first half of the ASCII characters then it would be better to filter out than in since most of the 128 characters are displayable. Starting at 32 filters all control characters out. BUT a neat trick would be to use a little string array to hold the names of the first 32:
char CharCodes[][4] = {"NUL", "SOH", "STX", "ETX", "EOT", "ENQ",
 "ACK", "BEL", "BS" , "TAB", "LF" , "VT" ,
 "FF" , "CR" , "SO" , "SI" , "DLE", "DC1",
 "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
 "CAN", "EM" , "SUB", "ESC", "FS" , "GS" ,
 "RS" , "US" };


and then check if i <32 print CharCodes[i] else print (char) i. That way you would have all of the codes printed from 1 to 128 (though really that should be 0 to 127 for the true "first half of ascii characters").
Was This Post Helpful? 0
  • +
  • -

#10 dragonsword1  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 02-February 07

Re: how to make a text file

Posted 02 June 2007 - 04:02 PM

well thanks to you all i dun knw how to express it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1