This program is supposed to read an input file stream and transfer the file to an output stream. It's also supposed to catch all instances of "cin <<" and replace them with "cin >>". It's also supposed to replace "cout >>" with "cout << " The number of whitespaces between the cin/cout streams and the insertion/extraction operators is supposed to be an indeterminate amount for each case. Here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include <fstream>
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
using namespace std;
//Rewrites the file from the input stream to the output stream with all instances of "cin <<" and "cout >>"
//corrected. There can be any number of whitespaces between cin/cout and the insertion/extraction operator
void file_transfer(ifstream& in_stream, ofstream& out_stream);
//Takes the next character to be proccessed from file and checks if it changes whether the text is transitioning
//from being inside quotation marks to being outside quotation marks or vice versa.
//in_quotes is true if the current text is in quotes and false otherwise assuming that the variable input for parameter
//in_quotes gives the correct state the text is in at the time the function is called (i.e. whether the text is in quotes or outside of quotes)
void check_quotes(char next, bool in_quotes);
//clears white spaces inbetween two non-whitespaces in an input file stream
void clear_spaces(ifstream& in_stream);
int main()
{
ifstream in_stream;
in_stream.open("test3.txt");
if(in_stream.fail())
{
perror("Input stream failed to open");
cin.get();
exit(1);
}
ofstream out_stream;
out_stream.open("test2.txt");
if(out_stream.fail())
{
perror("Input stream failed to open");
cin.get();
exit(1);
}
file_transfer(in_stream, out_stream);
in_stream.close();
out_stream.close();
cout << "End of the program.\nEnter key to continue.";
cin.get();
return 0;
}
void file_transfer(ifstream& in_stream, ofstream out_stream)
{
char next;
long i(0);
bool in_quotes = false;
cin.get(next);
while(!in_stream.eof())
{
switch(next)
{
case 'c':
{
if(!in_quotes)
{
i=1;
out_stream << next;
}
}
case 'i':
{
if(i==1)
{
i=2;
}
else
{
i=0;
}
out_stream << next;
}
case 'n':
{
if(i==2)
{
i=3;
}
else
{
i=0;
}
out_stream << next;
clear_spaces(in_stream);
}
case 'o':
{
if(i==1)
{
i=6;
}
else
{
i=0;
}
out_stream << next;
}
case 'u':
{
if(i==6)
{
i=7;
}
else
{
i=0;
}
out_stream << next;
}
case 't':
{
if(i==7)
{
i=8;
}
else
{
i=0;
}
out_stream << next;
clear_spaces(in_stream);
}
case '<':
{
if(i==3)
{
i=4;
}
else if(i==4)
{
i=0;
out_stream << ">>";
}
else
{
out_stream << next;
}
}
case '>':
{
if(i==8)
{
i=9;
}
else if(i==9)
{
out_stream << "<<";
}
else
{
out_stream << next;
}
}
default:
{
if(i==4)
{
out_stream << '<';
}
else if(i==9)
{
out_stream << '<';
}
out_stream << next;
i=0;
}
}
cin.get(next);
check_quotes(next, in_quotes);
}
}
void check_quotes(char next, bool in_quotes)
{
if(next == '"')
{
in_quotes = !in_quotes;
}
return;
}
void clear_spaces(ifstream& in_stream)
{
char next;
do
{
in_stream.get(next);
}while(isspace(next));
in_stream.putback(next);
return;
}
|
I realize it's messy. I was wondering if there's an easier way to implement the function file_transfer because although I think I have the right idea I think that there's got to be an easier way of doing this than how I attempted to do it thus far. Secondly, I am getting a function because of my file_transfer function. The error is:
1>test2.obj : error LNK2019: unresolved external symbol "void __cdecl file_transfer(class std::basic_ifstream<char,struct std::char_traits<char> > &,class std::basic_ofstream<char,struct std::char_traits<char> > &)" (?file_transfer@@YAXAAV?$basic_ifstream@DU?$char_traits@D@std@@@std@@AAV?$basic_ofstream@DU?$char_traits@D@std@@@2@@Z) referenced in function _main
1>C:\Users\Paul\documents\visual studio 2010\Projects\test2\Debug\test2.exe : fatal error LNK1120: 1 unresolved externals
Before I compile there's a red squiggly line under the function call to file_transfer. When I hover over it it says "More than one instance of of overloaded function "file_transfer" matches the argument list". I thought this meant that there was another function with the same name that takes the same arguments so I experimented with changing the name of it (in the function call, header, and definition) and that hasn't solved the problem.
Even if someone comes up with an alternate suggestion for how to implement the file_transfer function, I would still like to know, if possible, why I am getting a compilation error as the code is right now so that I would know for future references what it is that I did wrongly.
Thank you for taking the time to read this.