2016-12-30 9 views
0

ifstreamで読み込まれたファイルをナビゲートする必要があります。正確には、数行を読んだあと、この正確な場所に戻るために、前にあった場所を保存する必要があります。 私はそのような何かを試してみました:streamposを使用したファイル内のナビゲーション?

ifstream baseINTING("base.txt"); 
streampos position1, position2, position3; 

position1 = baseINTING.tellg(); 

その後、私は数行読んだ上:

getline(baseINTING, buffer2); 

をして戻ることを試みた:

baseINTING.seekg(position1); 

しかし、それは動作しません。 。誰か助けてくれますか?私は本当にその決定を必要とします。

これは完全なコードです:

#include <iostream> 
#include <fstream> 
#include <string> 
#include <direct.h> 
#include <cstdlib> 


using namespace std; 



void anyNextOne(ifstream* newFile) 
{ 
    ifstream baseINTING("base.txt"); 
    int j; 

    string buffer2; 
    streampos position1; 


    getline(baseINTING, buffer2); 
    getline(baseINTING, buffer2); 

    position1 = baseINTING.tellg(); 


    for(j=0; j<5; j++) 
    { 
     getline(baseINTING, buffer2); 

     cout<<buffer2<<endl; 
     cout<<position1<<endl; 

     baseINTING.seekg(position1); 

    } 

} 
int main() 
{ 
    ifstream one("one.txt"); 
    anyNextOne(&one); 


    return 0; 
} 

そしてexamplary出力:(4人用)

成分(4人用)

(...)

"base.txt" ファイルの内容(4人用):teelgは何の値を返したかどうか

Easy Pizza Recipe 
Dough: 
Ingredients (for 4 people) 
• 1 cup of warm water 
• 3 1/2 cups of flour 
• 2 tablespoons of olive oil 
• 2 teaspoons of honey 
• 1 teaspoon of salt 
• 1 teaspoon of yeast 
Method: 
1. Put warm water into a bowl. Add salt and honey and mix with a spoon. Add yeast, mix and let it sit for about 10 minutes. 
2. add flour and olive oil and start mixing. 
3. Let it sit for about another hour. 
4. Put on some tomato sauce. 
5. Put on your pizza topping (some green peppers, mushrooms, ketchup, cheese, sausage, salt and pepper) 
6. Bake the pizza in you oven at 200 C for about 20 to 25 minutes. 
+0

あなたが与えたコードに何か間違いはありませんでした。どのように動作しないのか説明できますか? –

+0

私はそれを下に掲示しました;) –

+0

あなたの質問を編集し、そのコードをそれに埋め込むことがより適切であると思われます。あなたのプロブレムについて、そのコードの出力は何ですか、あなたがseekg()を呼び出すたびに同じ場所に行かないと言っていますか? –

答えて

-1

チェックあなたが期待していることと、あなたがファイルの間違った場所にいるかどうか。私はhexeditor/hexdumpをお勧めします

+0

これは、ラインとディスプレイを最初に正しく表示するように機能しますが、次回はラインの始めに数文字をトリミングします。 –

+0

ラインは です。成分(4人用) 最初に正しく表示された後に表示されます。 baseINTING.seekg(position1); 表示: dients(4人用) –

0
#include <iostream> 
#include <fstream> 
#include <string> 
#include <direct.h> 
#include <cstdlib> 


using namespace std; 



void anyNextOne(ifstream* newFile) 
{ 
    ifstream baseINTING("base.txt"); 
    int j; 

    string buffer2; 
    streampos position1; 


    getline(baseINTING, buffer2); 
    getline(baseINTING, buffer2); 

    position1 = baseINTING.tellg(); 


    for(j=0; j<5; j++) 
    { 
     getline(baseINTING, buffer2); 

     cout<<buffer2<<endl; 
     cout<<position1<<endl; 

     baseINTING.seekg(position1); 

    } 

} 
int main() 
{ 
    ifstream one("one.txt"); 
    anyNextOne(&one); 


    return 0; 
} 
関連する問題