السلام عليكم و رحمة الله و بركاته ...
لقد قمت بكتابة برنامج يقوم بعمل نشرة لأحد المواقع ، و هذا البرنامج يقوم باخذ ملفات txt و أيضا ملفات html و من هذه الملفات يقوم بإنشاء النشرة البريدية ، و النشرة هي عبارة عن جدول مقسم إلى فئات و تحت كل فئة المنتجات الخاصة بتلك الفئة.
المشكلة الأولى : البرنامج يعمل بشكل صحيح و لكن فقط لخلية واحدة من الجدول (لمنتج واحد) ، و عندما يقوم البرنامج باستدعاء الدالة write_cell مرة أخرى يقوم البرنامج بتنفيذ بعض الأوامر و يتجاهل الأخرى.
المشكلة الثانية : البرنامج لا يظهر الكلمات العربية بشكل صحيح (مع العلم بأن كود الhtml كتب على الويندوز و كان يظهر بشكل صحيح في internet explorer) ، و لقد قرأت المشاركة السابقة بالنسبة إلى الكتابة باللغة العربية و لكن الحل كان يستخدم windows.h ، فما هي الطريقة للكتابة بشكل صحيح على لينكس "يوبنتو" ؟
#include <iostream>
#include <fstream>
using namespace std;
// a function to write the begining of the template to newsletter.htm
void write_begin (ifstream & begin, ofstream & newsletter)
{
char ch;
begin.get(ch);
while (!begin.eof())
{
newsletter<<ch;
begin.get(ch);
}
}
// a function to write the end of the template to newsletter.htm
void write_end (ifstream & end, ofstream & newsletter)
{
char ch;
end.get(ch);
while (!end.eof())
{
newsletter<<ch;
end.get(ch);
}
}
// a function to write the category of the products to newsletter.htm
void write_title (ifstream & title, ofstream & newsletter)
{
// getting the name of the category
string category;
cout<<"Enter the name of the category : \n";
cin>>category;
int count = 0;
char ch;
title.get(ch);
// writing chars to newsletter.htm up until it reaches
// the place where to get the name of the category
while (!title.eof())
{
if (count >= 2)
break;
if (ch == '>')
count ++;
newsletter<<ch;
title.get(ch);
}
newsletter<<endl<<category;
// resumming the writing process
title.get(ch);
while (!title.eof())
{
newsletter<<ch;
title.get(ch);
}
}
// a function to write a cell (a product) to the table of products
void write_cell (ifstream & cell, ofstream & newsletter)
{
// getting the product info.
string product, source, link;
cout<<"Enter the name of the product : \n";
cin>>product;
cout<<"Enter the image source : \n";
cin>>source;
cout<<"Enter the product link : \n";
cin>>link;
char qoute = 2;
char ch;
cell.get(ch);
// writing chars to newsletter.htm up until it reaches
// the place to write the product link
while (!cell.eof())
{
if (ch == 'f')
{
newsletter<<ch;
break;
}
newsletter<<ch;
cell.get(ch);
}
// writing the product link
newsletter<<"="<<qoute<<link<<qoute;
// writing chars to newsletter.htm up until it reaches
// the place to write the image source
cell.get(ch);
while (!cell.eof())
{
if (ch == 'c')
{
newsletter<<ch;
break;
}
newsletter<<ch;
cell.get(ch);
}
// writing the image source
newsletter<<"="<<qoute<<source<<qoute;
// writing chars to newsletter.htm up until it reaches
// the place to write the product name
cell.get(ch);
while (!cell.eof())
{
if (ch == 'c')
{
break;
}
newsletter<<ch;
cell.get(ch);
}
// writing the product name
newsletter<<endl<<product;
// resumming the writing process
cell.get(ch);
while (!cell.eof())
{
newsletter<<ch;
cell.get(ch);
}
}
//// a function to write the categories and the products to newsletter.htm
//void write (ifstream & cell, ifstream & title, ofstream & newsletter)
//{
//char ch;
//int n, count = 0;
//cout<<"Enter the number of categories : \n";
//cin>>n;
//for (int i=0; i<n; i++)
//{
//write_title (title, newsletter);
//cout<<"Enter (Y/N) if you would like to enter a product : \n";
//cin>>ch;
//while (ch == 'y' || ch == 'Y')
//{
//if (count == 0)
//newsletter<<"<tr>";
//if (count >= 3)
//{
//newsletter<<"</tr>";
//count = 0;
//}
//write_cell (cell, newsletter);
//count ++;
//cout<<"Enter (Y/N) if you would like to enter a product : \n";
//cin>>ch;
//}
//}
//}
// a function to write the categories and the products to newsletter.htm
void write (ifstream & cell, ifstream & title, ofstream & newsletter)
{
char ch = 'y', temp;
int count = 0 ;
cout<<"Enter (Y/N) for a category : \n";
cin>>temp;
// outer loop for the categories
while (temp == 'y' || temp == 'Y')
{
write_title (title, newsletter);
// inner loop for the products
while (ch == 'y' || ch == 'Y')
{
if (count == 3)
{
newsletter<<"\n </tr> \n";
count = 0;
}
cout<<"Enter (Y/N) if you would like to enter a product : \n";
cin>>ch;
if (ch == 'y' || ch == 'Y')
{
if (count == 0)
newsletter<<"\n <tr> \n";
write_cell (cell, newsletter);
count ++ ;
}
else
break;
}
cout<<"Enter (Y/N) for a category : \n";
cin>>temp;
ch = 'y';
}
}
int main()
{
cout<<"Please create an html file named newsletter (newsletter.htm) in the same directory of this program\n\n";
ifstream begin("begin.txt");
ifstream end("end.txt");
ifstream cell("cell.txt");
ifstream title("title.txt");
ofstream newsletter("newsletter.htm");
// writting the begining
write_begin (begin, newsletter);
// creating the table of products
write (cell, title, newsletter);
// writing the end
write_end (end, newsletter);
return 0;
}