
21-09-2008, 11:56
|
|
|
|
חבר מתאריך: 03.08.07
הודעות: 47
|
|
|
טוב.
טוב תודה על ה מדריך על ה STRING זה ענה לי על השאלה.
המדריכים האלה בכלל לא עוזרים לי לגבי הקריאה מקבצים..זה בדיוק מה שאני עשיתי וזה לא קורא כמו שצריך. לכתוב לקובץ אני מצליח אבל לקרוא זה פשוט לא קורה כמו שצריך זה קורא לי בלי רווחים ובלי סוף שורה..נגיד יש קובץ שבו כתוב :
hi my name is ayal
im a nice boy
זה קורא אותו ככה:
himynameisayalimaniceboy
אז מה עשינו בזה? עדיף כבר להשתמש ב FILE של סי הרבה יותר נוח.
קוד:
// files.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <fstream>
#include<cstdio>
#include<string>
using namespace std;
int main(int argc, char* argv[])
{
string str;
char c,d,filename[20] ,array[100];
ifstream myfile1;ofstream myfile;
int i;
start:
for(i=0;i<101;i++)array[i]=' ';
str=' ';
printf("R-reading file,W- write a file,Q -exit\n");
cin>>c;
switch(c)
{
case 'w':
start1:
cout<<"write the name of the file you wish to write to:"<<endl;
cin>>filename;
myfile.open(filename);
if (myfile.is_open())
{
cout<<"write the new context of the file :"<<filename<<endl;
cin>>str;
myfile<<str;
myfile.close();
}
else{cout<<"unable to open file for writing"<<endl;goto start1;}
break;
case 'r':
start2:
cout<<"write the name of the file you wish to read:"<<endl;
cin>>filename;
myfile1.open(filename);
if (myfile1.is_open())
{
str=' ';
i=0;
while(!myfile1.eof()){myfile1>>array[i];i++;}
myfile1.close();
cout<<"the context of the file is :"<<endl;
for(i=0;i<101;i++)cout<<array[i];
}
else{cout<<"unable to open file for reading"<<endl;goto start2;}
break;
case 'q':
return(0);
default: goto start;
}
goto start;
return(0);
}
תתמקדו ב CASE - R כי שם הבעייה .
נערך לאחרונה ע"י ayalshimoni בתאריך 21-09-2008 בשעה 11:59.
|