Reputation: 103
I would really like some help in figuring this out.
I have two files, one txt file and one xls file, in the txt file it contains a list of student names and their ID. In the xls file it contains alphabet letters and some numbers assigned to them.
I need to create a new txt file for every student, with their ID as filename. And in this file, I need to show the numbers that are assigned to the alphabet which makes up the student's name. Please help me, really cant figure it out within the time given txt filexls file
What I've been able to do so far using C++ is to read the data from the txt file and i convert the xls file into a txt file as well and read from that as well. Do i need to store the data in an array or its easier to just convert to the resulting file
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputa("Students.txt");
ifstream inputb("Alphabet.txt");
if (!inputb.is_open()) std::cout << "Error: File Not Open" << '\n';
/* ofstream output;
output.open("output.txt");
This was just to see if it was getting anything
*/
string name;
string id;
string alphabet;
string number;
while (inputa >> name >> id || inputb >> alphabet >> number) {
/* here i planned to compare the strings using string::find, but it doesnt work */
}
system("pause");
return 0;
}
Upvotes: 1
Views: 405
Reputation: 1687
At the time of writing, you still haven't given an example of what the file that holds IDs and student names looks like, so I might have gotten the ordering wrong, but you should get the idea.
I would just read the alphabet file in, storing the encoding, followed by writing out all of the student files in one step:
#include <cstdlib>
#include <fstream>
static int encoding_map[256] = {0};
bool load_encoding(const char* file_name)
{
std::ifstream file;
file.open(file_name);
if (!file.is_open()) return false;
char from = 0;
int to = 0;
while (file >> from >> to) encoding_map[(int)from] = to;
return true;
}
bool write_student_files(const char* src_file)
{
std::ifstream file;
file.open(src_file);
if (!file.is_open()) return false;
std::string id;
std::string name;
std::ofstream outfile;
while (file >> id >> name)
{
outfile.open(id + ".txt");
for (char c : name) outfile << encoding_map[(int)c];
outfile << '\n';
outfile.close();
}
return true;
}
int main()
{
load_encoding("encoding.txt");
write_student_files("students.txt");
return EXIT_SUCCESS;
}
Here is my encoding file (alphabet file), encoding.txt
:
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
i 9
j 10
k 11
l 12
m 13
n 14
o 15
p 16
q 17
r 18
s 19
t 20
u 21
v 22
w 23
x 24
y 25
z 26
and here is my students file, students.txt
:
0 bob
1 susan
2 joe
3 becky
If I compile the code with g++ -std=c++11 main.cpp
and run it, I get 4 new files in the same directory:
0.txt
1.txt
2.txt
3.txt
The contents of 0.txt
, for example, is:
2152
You can mess with the spaces if you want, but, with my simple encoding:
b => 2
o => 15
b => 2
meaning the output is correct(though not very pretty).
Clearly, this does not do much error handling, doesn't handle names with multiple words, etc., but you should be able to figure it out from here.
Upvotes: 1