473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

write/read to file, but where?

bluegreenisland
Hi everyone,
I am new to this, so am bound to make blunders ~just let me know & I'll correct them.
I am hoping to learn how to write to a file, make changes to the data, and read.
I have a class definition in a .h file, the member functions definitions in a .cpp file, and a driver program which has two functions besides main(): writeFile() and readFile().
My problem is that I don't know what to do now! I tried declaring an object of my class in main() and using that object to pass a string called "a" to my file. Hmm. No go.
Can I have a clue, please?

int main()
{
AnagramCheck dataset1(3, "tan", 4, " ant");

writeFile();
readFile();

return 0;
}

void writeFile()
{
ofstream outFile("anagrams.dat", ios::out);//creates an ofstream object

if(!outFile)
{
cerr << "File could not be opened." << endl;
exit(1);
}

outFile << dataset1.geta();

outFile.close();
}

void readFile()
{
ifstream inFile("anagrams.dat", ios::in);//second argument (file mode) not necessary

if(!inFile)
{
cerr << "no" << endl;
exit(1);
}

while(inFile.peek() != EOF)
cout.put(inFile.get);

inFile.close();
}
Mar 28 '07 #1
4 1556
sicarie
4,677 Expert Mod 4TB
Depends on what you're trying to do. If you are trying to use the function to read a line from a file, you need to return that line (probably in a string, or pointer to a string), and if you are writing to the file, you need to pass the string (or pointer to character array) to the function.

But it also looks like you have that AnagramCheck, is that what you are trying to pass?
Mar 28 '07 #2
Hi Sicorie,
My instructions say that my program should accept two command line arguments: the first is the name of an input file and the second the output file. The input contains a number of data sets of 4 lines (int, string, int, string). Then I must generate an output file that for each data set does three processes (create new strings that convert the old to uppercase, determine how many words, and compare for "anagramness"). I pretty much have my functions, I think. I am to put the functions that manipulate the strings in a separate file, which is why I created a class. Was that wise? I wonder how to get the dataset into the file, and from where to call the functions. Does this make it clear? Here is my AnagramCheck.h file:


class AnagramCheck
{
private:
int aSize;
string a;
int bSize;
string b;

public:
AnagramCheck();//default constructor
AnagramCheck(int xSize, string x, int ySize, string y);
void setData(int xSize, string x, int ySize, string y);
int AnagramCheck::getaSize() const;
string AnagramCheck::geta() const;
int AnagramCheck::getbSize() const;
string AnagramCheck::getb() const;
int count_words(string c);
string make_upcase(string c);
int ckForChar(string a, string b);
bool are_anagrams(string a, string b);
};

Oh, and I hope you know how much you are appreciated!
Mar 28 '07 #3
sicarie
4,677 Expert Mod 4TB
I'm not sure how necessary an entire class would be for a program this size - but it's definitely on the edge, if you decided to, then all the better to get experience with classes and good programming practices.

To accept command-line input, the 'int main()' needs something in the parentheses - 'int argc, char* argv[]' Then you can access the things in argv[] as you need them (the string,int,string,int) as an array.

It seems that, as you're sort of stuck on the execution of your program, I would go back to the "algorithm board". You would call the three functions from your main after reading them in and properly initializing the Anagram type, and then you would need some communication between the results of the three functions in the Anagram class (either returning, or directly calling - which might involve moving the printToFile() to your Anagram class). One of the tricks is to not make it more complicated than it needs to be.

so my pseudocode would be

get input from user
make sure it's proper type (char * coming in, and you said 2 would be ints...)
initialize anagram
set as uppercase
determine words
compare 'anagramness'
write to file

does that help clarify?
Mar 28 '07 #4
Exactly what I needed. What a relief. Thank you!
Mar 28 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ellixis | last post by:
Hello, How can I use fwrite() and fseek() in order to write data in the middle (or anywhere else) of a file without overwriting existing data ? People told me that I should load the file into...
33
by: Nick Evans | last post by:
Hello there, I have been on and off learning to code (with python being the second language I have worked on after a bit of BASIC). What I really want to know is, if you are going to actually...
1
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open the file for writing: <CODE> public...
16
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
8
by: Patrik Malmström | last post by:
How do I read, write a file binary? I want to open, say, file.exe read it in to the program, then write it out to file2.exe. Like file copy, anyone have a code sample?
12
by: Nina | last post by:
Hi there, What is the maximum length that one line can hold in a text file using StreamWriter's Write or WriteLine method? If the string is too long for one line to hold, what will Write or...
0
by: Frederic Rentsch | last post by:
Hi all, Working with read and write operations on a file I stumbled on a complication when writes fail following a read to the end. 30L 'abcdefg' Traceback (most recent call last): File...
6
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
3
by: eholz1 | last post by:
Hello PHP Group, I am having trouble setting permissions correctly so that the magickwand api (php 5.2) can read and write images. I usually read a file from one directory, create a magickwand...
6
by: globalrev | last post by:
i ahve a program that takes certain textsnippets out of one file and inserts them into another. problem is it jsut overwrites the first riow every time. i want to insert every new piece of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.