-
ok this outputs about 100 numbers with the spaces
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
int main()
{
int num[120];
int counter;
int x;
ofstream outstream("digits2.dat");
if(outstream.fail())
{
cout<<"File Problem"<<endl;
exit(1);
}
srand(time(NULL));
for(counter=0; counter<100; counter++)
{
x=1 + rand() % 100;
outstream<<x; // this puts number into file and increments marker/counter to the end of the number
}// this is where it goes back and puts the next number and another space... does this 100 times
outstream.close();
return(0);
}
-
>>>yea I see some numbers in the file
awesome... see you are halfway there. No reason to get frustrated.
Also...
.put() is used with your ofstream... sorry bout that. I meant it in my head but wrote something different.
Examine this ANSI complient code... how does it differ from your output to file code.
Code:
#include <fstream>
#include <iostream>
#include <stdlib>
using namespace std;
int main ()
{
// Local variables
char mychar; // Character Storage
int counter=0; // Integer counter
char array[5] = "1234";
ofstream outs; // output buffer
ifstream ins; // input buffer
outs.open("betazep.txt"); // open output file
if (outs.fail()) // error checking
{
cout << "Output file failure." << endl;
return 0;
} // end if
//********************************//
// Cout array
cout << array << endl << endl;
// Output array to file
while(counter < 5)
{
outs.put(array[counter]);
outs.put(' ');
cout << array[counter];
++counter;
}// end while
outs.close(); // close output file
// Copying from one file to another**************************
ins.open("betazep.txt"); // open input file
if (ins.fail())
{
cout << "Input file failure." << endl;
return EXIT_FAILURE; // as found in stdlib
} // end if
outs.open("venomgts550.txt"); // open output file
if (outs.fail()) // error checking
{
cout << "Output file failure." << endl;
return 0;
} // end if
while (!ins.eof())
{
ins.get(mychar);
if (mychar != ' ')
{
outs.put(mychar);
cout << "copying " << mychar << endl; // show what is getting copied
} // end if
} // end while... file copied without spaces
ins.close();
outs.close();
return 0;
} // end main
compile that and see how it works... also try to increase the array size by one and add one more number and then go down and increase the counter < 5 to counter < 6.
Notice that the file manipulation can handle any file size. It is only limited by the end of file.
-
Its a little confusing to me but it looks like it open betazip.txt then copies it to venomgts550.txt
-
good... now lets clean it up... try this on your compiler
#include <iostream>
#include <fstream>
#include <stdio>
using namespace std;
int main()
{
int x, counter;
ofstream outstream("digits2.dat");
if(outstream.fail())
{
cout<<"File Problem"<<endl;
return 0;
}
for(counter=0; counter<100; counter++)
{
x=1 + rand() % 100;
outstream << x;
outstream.put(' ');
}
outstream.close();
return(0);
}
-
>>>Its a little confusing to me but it looks like it open betazip.txt then copies it to venomgts550.txt
It opens betazep.txt and puts 1 2 3 4 into it and closes it.
Then it opens both files and copies betazep.txt over to venomgts550.txt without the spaces.
Like...
1234
Doesn't do much really. But the latter portion could copy an entire text file into another file.
-
-
I gotta clean my house. Try writing a seperate program that can read the input from the file and view them as integers.
Get it to do just one number and I will show you how to loop through the rest effectively.
Bye for tonight....
Betazep
-
ok thanx for all your help
good night
-
Can someone please help me I cant seem to get this input right
this is what i have so far
#include
#include
#include
#include
#include
#include
int main()
{
int array[120];
int x, counter;
ofstream outstream("digits2.dat");
if(outstream.fail())
{
cout<<"File Problem"<return 0;
}
srand(time(NULL));
for(counter=0; counter<100; counter++)
{
x=1 + rand() % 100;
outstream<outstream.put(' ');
}
outstream.close();
ifstream instream("digits2.dat");
instream>>x;
//----------------------------------------------------------------------------------------------------
char mychar;
int integer_counter;
int size_three_char_array[120];
for(int count=0; count<100; count++)
{
instream.get(mychar);
cout<outstream.put(' ');
if ( mychar != ' ')
{
size_three_char_array[integer_counter]; // put the number in an array at position zero for now
integer_counter++; // originally set to 0
}
else {
integer_counter = 0; // if we didn't find a number then we found a space, and we need to clear our counter
}
//----------------------------------------------------------------------------------------------------
}
instream.close();
return(0);
}
-
-
where have you gotten with this?
-
i tried to get the output to work
-
Code:
// Program opens a file filled with integer values seperated by spaces and finds the
// largest value within the file. Integers must be three digits or less, and the final
// integer must be followed by a space to have it included. (all which can be modified
// easily within the code)
# include<fstream>
# include<iostream>
# include <stdio>
using namespace std;
int main ()
{
// Local variables
ifstream ins;
char yourfile[100], mynum[4];
char mychar;
int count, temp, final = 0;
cout << "What is the file path/name: ";
cin >> yourfile;
ins.open(yourfile);
if (ins.fail())
{
while (ins.fail())
{
cout << "Failure opening input file. Check file path." << endl << endl
<< "You entered " << yourfile << endl
<< "Enter path/name (or X to quit): ";
cin >> yourfile;
if ((yourfile[0]=='X')||(yourfile[0]=='x'))
return 1;
ins.open(yourfile);
} // end while
}// end if
count = 0;
while (!ins.eof()) // go until file ends
{
ins.get(mychar);
cout << mychar;
if ((isdigit(mychar))&&(mychar!=' '))
{
mynum[count] = mychar;
++count;
} // end if
if (mychar == ' ')
{
count = 0;
temp = atoi(mynum);
if (temp > final)
{
final = temp;
}
mynum[0] = '\0';
mynum[1] = '\0';
mynum[2] = '\0';
} // end if
}// end while
ins.close();
cout << endl << endl <<"The largest number in the file was "
<< final;
return 0;
}
That does it pretty much. I cannot help you anymore. you put the two together and try to figure it out. There are other ways as well....