저는 C++을 처음 접했고 예 숙제입니다. 내 함수에서 if else 문 대신 switch 문을 고려하고 있습니다.파일 스트림에서 출력 파일 만들기
데이터를 조작 한 후에 입력 파일 스트림에서 읽은 정보를 출력 파일에 쓰려고합니다.
prgram은 파일에서 정보를 읽고 처리 한 다음 콘솔에 데이터를 표시하고 결과를 출력 파일에 기록하기 때문에 프로그램은 사용자에게 입출력을위한 파일 이름을 입력하도록 요청해야합니다 파일.
내 프로그램에서 파일을 만들 수 없습니다. 그것은 이미 존재하는 파일들과 함께 작동합니다.
내 프로그램이 없으면 파일을 생성하도록 도와주세요.
아, 그리고이 어떤 도움이 크게 감사합니다
C++입니다.
MY CODE :
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
char getNumber(char l);
int main()
{
string s1 = "D:\\Unisa\\Assignment_stuffs\\COS1512\\Assignment\\";
string inFile, outFile;
cout << " Please enter the input filename: ";
cin >> inFile;
cout << "\nPlease enter the output filename: ";
cin >> outFile;
string inFileAdd = s1 + inFile;
string inFileAdd2 = s1 + outFile;
ifstream in_stream;
ofstream out_stream;
in_stream.open(inFileAdd.c_str(), ios::in);
if (in_stream.fail())
{
cout << "Error!! Input file opening failed.";
exit(1);
}
out_stream.open(inFileAdd2.c_str(), ios::out);
if (out_stream.fail())
{
cout << "Error!! Output file opening failed.";
exit(1);
}
char next = ' ';
string letter;
while (!in_stream.eof())
{
in_stream.get(next);
while (next != '\n')
{
cout << next;
out_stream.put(next);
letter = letter + getNumber(next);
in_stream.get(next);
}
cout << " " + letter;
out_stream << " " + letter << endl;
letter = "";
cout << endl;
}
in_stream.close();
return 0;
}
char getNumber(char l)
{
if ((l == 'A') || (l == 'a') || (l == 'B') || (l == 'b') || (l == 'C') || (l == 'c'))
{
return '2';
}
else if ((l == 'D') || (l == 'd') || (l == 'E') || (l == 'e') || (l == 'F') || (l == 'f'))
{
return '3';
}
else if ((l == 'G') || (l == 'g') || (l == 'H') || (l == 'h') || (l == 'I') || (l == 'i'))
{
return '4';
}
else if ((l == 'J') || (l == 'j') || (l == 'K') || (l == 'k') || (l == 'L') || (l == 'l'))
{
return '5';
}
else if ((l == 'M') || (l == 'm') || (l == 'N') || (l == 'n') || (l == 'O') || (l == 'o'))
{
return '6';
}
else if ((l == 'P') || (l == 'p') || (l == 'Q') || (l == 'q') || (l == 'R') || (l == 'r') || (l == 'S') || (l == 's'))
{
return '7';
}
else if ((l == 'T') || (l == 't') || (l == 'U') || (l == 'u') || (l == 'V') || (l == 'v'))
{
return '8';
}
else if ((l == 'W') || (l == 'w') || (l == 'X') || (l == 'x') || (l == 'Y') || (l == 'y') || (l == 'Z') || (l == 'z'))
{
return '9';
}
}
프로그램이'D에서 파일을 만들 수있는 적절한 권한이 있습니까 : UNISA \ Assignment_stuffs \ COS1512 \ Assignment' \를? 존재하는 파일에 쓸 수있는 권한을 가질 수는 있지만 새로운 파일을 작성할 수는 없습니다. – lcs
ifstream 및 ofstream 클래스의 경우 ios :: in 및 ios :: out은 자동으로 각각 가정되므로 실제로 필요하지 않습니다. – SirDarius
코드를 최소화하고 별도의 조각을 개발하는 것이 중요합니다. 새로운 파일에 쓰는 가장 간단한 프로그램을 작성하십시오 *. 당신은 스스로 문제를 발견 할 수도 있고, 우리가 실행할 수있는 예를 가지고 문제를 발견 할 수도 있습니다. (내 돈은 @millsj에있다.) – Beta