File I/O User Defined .txt

Create (open) a user defined .txt file, write to .txt file strings, cl

Page 1 of 1

2 Replies - 2468 Views - Last Post: 19 March 2009 - 11:56 PM Rate Topic: -----

#1 Mejamz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-March 09

File I/O User Defined .txt

Posted 15 March 2009 - 01:39 PM

#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <cstdio>
#include <cctype>

using namespace std;

static string fileName = "No File Name";
const char *fileNamePtr = 0;
int length = fileName.length();
char fileNameHolder = NULL;
string encryptThis(string &inFileName);
string decryptThis(string &newFileName);

string getFileName();

// Main function to request user input for file I/O
//*****************************************************************************
int main()
{
FILE *original;
FILE *copy;
FILE *read;
FILE *write;

original = fopen("UserFileName.txt", "r");
copy = fopen("UserFileName.txt", "w");
read = fopen("UserFileName.txt", "r");
write = fopen("UserFileNAme.txt", "w+");

while(fileNameHolder !=-1)
{
fileNameHolder = fgetc(original);
if(!isspace(fileNameHolder))
{
fputc(fileNameHolder,copy);
}
}
/*close open files*/
fclose(original);
fclose(copy);
fclose(read);
fclose(write);
printf("The copy operation has been complete");
fileNamePtr = fileName.data();
cout << "File Name Default is: " << fileName
<< "\nFile Name Default after c_str is applied is: "
<< fileName.c_str() << endl;
system("PAUSE");
system("CLS");
getFileName();
system ("PAUSE");
system ("CLS");
cout << "File Name Default is: " << fileName
<< "\nFile Name Default after c_str is applied is: "
<< fileName.c_str() << endl;
system("PAUSE");
system("CLS");
cout << "File Name after encryption\n" << encryptThis(fileName) << endl;
system("PAUSE");
system("CLS");
cout << "File Name after decryption\n" << decryptThis(fileName) << endl;

return 0;
}

// Main support functions
// Encrypt the user file name
// by adding one value to each string character
//================================================================
string encryptThis(string &inFileName)
{

fileName = inFileName;
for (int i=0; i<fileName.length(); i++)
{
++fileName[i];
}
return fileName;
}
//*****************************************************************************
// Decrypt the user file name
// for use in display
//*****************************************************************************
string decryptThis(string &newFileName)
{
fileName = newFileName;
for (int i=0; i<fileName.length(); i++)
{
--fileName[i];
}

return fileName;
}
//*****************************************************************************
// Prompt the user and get a string input,
// return fileName,
// used in Getter.
//*****************************************************************************
string getFileName()
{
cout << "Please enter the file name" << endl;
cin.ignore(cin.rdbuf()->in_avail());
cin >> fileName;
//cin.getline(fileNameHolder, _MAX_PATH);
//getline(cin, fileName);
return fileName;
}
/*
I am difficulty with the difference between strings and char.
How can I open a user defined file, write to the file, read from the file, close the file and delete the file.
I am pulling my hair out trying to figure this out...Any suggestions?
So far, I can entera string, encrypt it, decrypt is, and display it...I cannot however, create and file the string .txt file.
:crazy:
*/

Is This A Good Question/Topic? 0
  • +

Replies To: File I/O User Defined .txt

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: File I/O User Defined .txt

Posted 15 March 2009 - 01:51 PM

:code:

This doesn't exactly apply to what you have, but it demonstrates File I/O, hope it helps:
#include <iostream>
#include <fstream.h>
#include <iomanip.h>
const int MAXNAME = 20;
const int NUMRECS = 5;

struct PayRecord {
    long id;
    char name[MAXNAME];
    float rate;
};

int main() {

    int i;
    PayRecord employee[NUMRECS] = {
                                  { 32479, "Abrams, B.", 6.72 },
                                  { 33623, "Bohm, P.", 7.54 },
                                  { 34145, "Donaldson, S.", 5.56 },
                                  { 35987, "Ernst, T.", 5.43 },
                                  { 36203, "Gwodz, K.", 8.73 }
                                  };

    ofstream FileMod;
    FileMod.open("test.txt");

    for (i = 0; i < NUMRECS; i++) {
        FileMod << setiosflags(ios::left)
                << setw(7) << employee[i].id
                << setw(15) << employee[i].name
                << setw(6) << employee[i].rate << endl;

        cout << setiosflags(ios::left)
             << setw(7) << employee[i].id
             << setw(15) << employee[i].name
             << setw(6) << employee[i].rate << endl;
    }

    FileMod.close();
    cin.get();
    return 0;
}


And welcome to DIC.

This post has been edited by Hyper: 15 March 2009 - 01:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 Mejamz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-March 09

Re: File I/O User Defined .txt

Posted 19 March 2009 - 11:56 PM

[quote name='Hyper' date='15 Mar, 2009 - 12:51 PM' post='573458']
:code:

This doesn't exactly apply to what you have, but it demonstrates File I/O, hope it helps:
[code="File I/O"]#include <iostream>
#include <fstream.h>
#include <iomanip.h>
const int MAXNAME = 20;
const int NUMRECS = 5;

struct PayRecord {
	long id;
	char name[MAXNAME];
	float rate;
};

int main() {

	int i;
	PayRecord employee[NUMRECS] = {
								  { 32479, "Abrams, B.", 6.72 },
								  { 33623, "Bohm, P.", 7.54 },
								  { 34145, "Donaldson, S.", 5.56 },
								  { 35987, "Ernst, T.", 5.43 },
								  { 36203, "Gwodz, K.", 8.73 }
								  };

	ofstream FileMod;
	FileMod.open("test.txt");

	for (i = 0; i < NUMRECS; i++) {
		FileMod << setiosflags(ios::left)
				<< setw(7) << employee[i].id
				<< setw(15) << employee[i].name
				<< setw(6) << employee[i].rate << endl;

		cout << setiosflags(ios::left)
			 << setw(7) << employee[i].id
			 << setw(15) << employee[i].name
			 << setw(6) << employee[i].rate << endl;
	}

	FileMod.close();
	cin.get();
	return 0;
}


And welcome to DIC.
[/quote]
So, I create a file with any file name for default...then get new file name from user?
Insert it into the file. Read it from the file and close the file?
I will try that.
Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1