سؤال

وبالتالي فإن المعلم قد طرحت هذه المهمة:

كنت قد استأجرت المتحدة شبكة قيادة لإنفاذ القانون ، و كنت قد أعطيت الملفات التي تحتوي null سيفيرس عليك فك تشفير.

وذلك لأول ملف معين (على سبيل المثال) كل رسالة هو الصحيح (أي:'hielqlpo' مرحبا (على افتراض انك تبدأ مع الحرف الأول).سؤالي الأول هو: كيف يمكنني قراءة في ملف ؟ المستند على سطح المكتب في مجلد وملف اسمه document01.البكاء.أنا لست متأكدا من الأمر احتاج إلى وضع هذا الملف في البرنامج.

أنا أيضا لا مفرط متأكد من كيفية انتزاع رسالة تخطي رسالة ، ولكن بصراحة كنت ترغب في العبث مع هذا من قبل وأنا بعد هذا السؤال!حتى الآن...سؤالي هو كما جاء في العنوان:كيف يمكنك انتزاع الملف للقراءة في C++?

إذا كان هذا يحدث فرقا (كما أنا متأكد من أن يفعل), أنا باستخدام Visual C++ 2008 Express Edition (لأنه حر و أنا أحب ذلك!لقد تعلق أيضا ما لدي حتى الآن ، يرجى أن نضع في اعتبارنا أنه -جدا - الأساسية...وأضفت getchar(); في النهاية حتى عندما لا تعمل بشكل صحيح النافذة تبقى مفتوحة حتى أستطيع أن أرى ذلك (كما البصرية Express يميل إلى إغلاق النافذة في أقرب وقت كما انها يتم تشغيل.)

مدونة حتى الآن:

#include<iostream>

using namespace std;

int main()
{
    while (! cin.eof())
    {
        int c = cin.get() ;
        cout.put(c) ;
    }
    getchar();
}

PS:وأنا أدرك أن هذا الرمز الاستيلاء و يضع بها كل شخصية.الآن هذا جيد, مرة واحدة يمكنني قراءة في الملف أعتقد أنني يمكن العبث معها من هناك.أنا أيضا بدس في كتاب أو اثنين عندي على C++ أن نرى أي شيء ينبثق وصرخات "اختيار لي!" شكرا مرة أخرى!

تحرير::أيضا غريبة, هل هناك طريقة إدخال الملفات التي تريد ؟ (أولا-هاء.:

char filename;
cout << "Please make sure the document is in the same file as the program, thank you!" << endl << "Please input document name: " ;
cin >> filename;
cout << endl;
ifstream infile(filename, ios::in);

هذا الكود لا يعمل.أنه يطلق النار مرة أخرى خطأ قول شار لا يمكن تحويلها إلى const char *.كيف يمكن أن هذه المشكلة تكون ثابتة ؟

تحرير 2:ناهيك عن جزء 2, لقد وجدت ذلك!شكرا مرة أخرى على المساعدة!

هل كانت مفيدة؟

المحلول 5

لقد اكتشفت ذلك!أن نكون صادقين لا إجابة واحدة ساعد كانت مزيج من الثلاثة ، بالإضافة إلى تعليقات منها.شكرا جزيلا لكم جميعا!لقد تعلق رمز اعتدت, وكذلك نسخة من الوثيقة.البرنامج يقرأ في كل حرف ، ثم يبصق على فك رموز النص.(أي:1 ساعة.e0l/lqo مرحبا) الخطوة التالية (رصيد إضافي) هو إعداده حتى مدخلات المستخدم كيف العديد من الشخصيات لتخطي قبل القراءة في الحرف التالي إلى المدخلات.

هذه الخطوة أنوي القيام به في بلدي, ولكن مرة أخرى, شكرا جزيلا لكم على كل المساعدة من الجميع!تثبت مرة أخرى كيف رهيبة هذا الموقع هو سطر واحد من التعليمات البرمجية في كل مرة!

تحرير::قانون تعديل يقبل إدخال المستخدم ، وكذلك السماح لاستخدامات متعددة بدون ترجمة (أنا أدرك أنه يبدو كبيرا قذرة الفوضى ولكن هذا هو السبب في انه للغاية علق...لأنه في رأيي أنها تبدو جميلة ونظيفة)

#include<iostream>
#include<fstream>   //used for reading/writing to files, ifstream could have been used, but used fstream for that 'just in case' feeling.
#include<string>    //needed for the filename.
#include<stdio.h>   //for goto statement

using namespace std;

int main()
{
    program:
    char choice;  //lets user choose whether to do another document or not.
    char letter;  //used to track each character in the document.
    int x = 1;    //first counter for tracking correct letter.
    int y = 1;    //second counter (1 is used instead of 0 for ease of reading, 1 being the "first character").
    int z;        //third counter (used as a check to see if the first two counters are equal).
    string filename;    //allows for user to input the filename they wish to use.
    cout << "Please make sure the document is in the same file as the program, thank you!" << endl << "Please input document name: " ;
    cin >> filename; //getline(cin, filename);
    cout << endl;
    cout << "'Every nth character is good', what number is n?: ";
    cin >> z;   //user inputs the number at which the character is good. IE: every 5th character is good, they would input 5.
    cout << endl;
    z = z - 1;  //by subtracting 1, you now have the number of characters you will be skipping, the one after those is the letter you want.
    ifstream infile(filename.c_str()); //gets the filename provided, see below for incorrect input.
    if(infile.is_open()) //checks to see if the file is opened.
    {
        while(!infile.eof())    //continues looping until the end of the file.
        {   
                infile.get(letter);  //gets the letters in the order that that they are in the file.
                if (x == y)          //checks to see if the counters match...
                {
                    x++;             //...if they do, adds 1 to the x counter.
                }
                else
                {
                    if((x - y) == z)            //for every nth character that is good, x - y = nth - 1.
                    {
                        cout << letter;         //...if they don't, that means that character is one you want, so it prints that character.
                        y = x;                  //sets both counters equal to restart the process of counting.
                    }
                    else                        //only used when more than every other letter is garbage, continues adding 1 to the first
                    {                           //counter until the first and second counters are equal.
                        x++;
                    }
                }
        }
        cout << endl << "Decryption complete...if unreadable, please check to see if your input key was correct then try again." << endl;
        infile.close();
        cout << "Do you wish to try again? Please press y then enter if yes (case senstive).";
        cin >> choice;
        if(choice == 'y')
        {
            goto program;
        }
    }
    else  //this prints out and program is skipped in case an incorrect file name is used.
    {
        cout << "Unable to open file, please make sure the filename is correct and that you typed in the extension" << endl;
        cout << "IE:" << "     filename.txt" << endl;
        cout << "You input: " << filename << endl;
        cout << "Do you wish to try again? Please press y then enter if yes (case senstive)." ;
        cin >> choice;
        if(choice == 'y')
        {
            goto program;
        }
    }
    getchar();  //because I use visual C++ express.
}

تحرير:::

حاولت إدخال النص ، ولكن لم أستطع الحصول عليه أن يخرج الحق ، فإنه أبقى علاج بعض الشخصيات مثل الترميز (أي علامة اقتباس أحادية على ما يبدو هو ما يعادل قيادة جريئة) ، ولكن هل يمكن أن مجرد محاولة وضع في "0h1e.l9lao" بدون أقواس في .txt و يجب أن تعطي نفس النتيجة.

شكرا مرة أخرى للجميع على المساعدة!

نصائح أخرى

لقيام عمليات الملفات، تحتاج تشمل الصحيح:

#include <fstream>

وبعد ذلك، في وظيفة الرئيسية الخاصة بك، يمكنك فتح دفق ملف:

ifstream inFile( "filename.txt", ios::in );

وأو للإخراج:

ofstream outFile( "filename.txt", ios::out );

ويمكنك بعد ذلك استخدام INFILE كما تستخدم CIN، وOUTFILE كما تستخدم cout. لإغلاق الملف عند الانتهاء:

inFile.close();
outFile.close();

[تحرير] تشمل تقديم الدعم لوسائط سطر الأوامر [تحرير] الثابتة الممكن تسرب الذاكرة [تحرير] ثابت على مرجع مفقود

#include <fstream>
#include <iostream>

using namespace std;

int main(int argc, char *argv){
    ifstream infh;  // our file stream
    char *buffer;

    for(int c = 1; c < argc; c++){
        infh.open(argv[c]);

        //Error out if the file is not open
        if(!infh){
            cerr << "Could not open file: "<< argv[c] << endl;
            continue;
        }

        //Get the length of the file 
        infh.seekg(0, ios::end);
        int length = infh.tellg();

        //reset the file pointer to the beginning
        is.seekg(0, ios::beg);

        //Create our buffer
        buffer = new char[length];

        // Read the entire file into the buffer
        infd.read(buffer, length);

        //Cycle through the buffer, outputting every other char
        for(int i=0; i < length; i+= 2){
            cout << buffer[i]; 
        }
        infh.close();
    }
    //Clean up
    delete[] buffer;
    return 0;
}

ويجب أن تفعل خدعة. إذا كان الملف كبير للغاية، وربما كنت لا ينبغي تحميل الملف بأكمله في المخزن المؤقت.

وعلى الرغم من أن تتم الإجابة queston الخاص بك، اثنين من نصائح قليلا: 1) بدلا من عد x و y لمعرفة ما إذا كنت على الغريب أو حتى الحرف الذي يمكن القيام بما يلي:

int count=0;
while(!infile.eof())
{       
    infile.get(letter);
    count++;
    if(count%2==0)
    {
        cout<<letter;
    }
}

و٪ يعني أساسا "تبقى عند مقسوما، 'حتى 11٪ 3 هي سنتان. في ما سبق أنه يعطي الغريب أو حتى.

2) على افتراض انك تحتاج فقط لتشغيله على ويندوز:

system("pause");

وسيجعل نافذة تبقى مفتوحة في حين ان الانتهاء من تشغيل بحيث لا تحتاج إلى getchar الماضي () دعوة (قد تضطر إلى #include<Windows.h> لهذا العمل).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top