2014-07-14 3 views
-1

데이터를 추가하는 응용 프로그램을 만들고 있지만 특정 데이터가 이미 추가되어 있는지 확인한 다음 데이터를 추가하려고합니다. 그러면 파일에 추가되지 않습니다. 진지한 답변에 미리C++ 해당 파일이없는 경우 파일에 단어 추가

void checklog(const std::string& input){ 
    ifstream iFile("C:\\Users\\Seann\\Documents\\storedData\\data.txt"); 
    string line; 
    while(getline(iFile, line)){ 
     if(input!=line){ 
     iFile.close(); 
     updateLog(input);} 
    } 
} 

감사 : 여기 내가 가지고 있지만 지금까지 그것의, 오류 작동하지하지만 그것은 단지 이미 존재하는 데이터를 추가하는 것이다. 그런

std::vector<std::string> fileInput; 

int i = 0; 

while (std::getline(iFile, fileInput[i]) 
i++; 

i = 0; 
bool found_string = false; 

while (i < input.size()) 
{ 
if (string_that_you_wanted_to_compare == fileInput[i]) 
found_string = true; 
} 

if (found_string) 
{ do whatever } 

또는 뭔가 : 당신이 단어에 대한 모든 라인을 확인하려면

+0

파일은 어떻게 생겼습니까? – 0x499602D2

+0

파일에 한 줄에 한 단어가 있다고 가정하면 읽은 가장 최근 항목과 일치하지 않는 단어를 즉시 추가합니다. 설명에 따르면 * all * 항목에 일치 항목이없는 경우에만 추가하려고합니다. – WhozCraig

+1

루프에서 빠져 나오면 첫 줄을 찾으면 입력과 같지 않습니까? – dari

답변

0

if ... else 시도는 읽는 첫 번째 줄에서 여전히 결정을 내리고 단어를 찾을 때까지 반복하지 않습니다. 이것을 시도하십시오 :

void checklog(const std::string& input){ 
    ifstream iFile("data.txt"); 
    string line; 
    bool found = false; 
    while (!found && getline(iFile, line)){ 
     if (input == line) 
      found = true; 
    } 
    iFile.close(); 
    if (found) 
     updateLog(input); 
} 
+0

정말 고마워요! – user3740948

1

,이 같은 것을 만들어야합니다.

+0

괜찮아요. 내 함수에 추가하려고했지만 오류가 발생했습니다. void checklog (const std :: string & input) { \t ifstream iFile ("C : \\ 사용자 \\ Seann \\ Documents \\ storedData \\ data.txt"); \t std :: vector fileInput; \t int i = 0; \t 동안 (표준 :의 getline (IFILE, fileinput 함수 [I]) { \t \t 내가 ++;} \t I = 0; \t 부울 found_string = 거짓; \t 동안 (나는 fileinput 함수를 <.크기()) { \t \t \t 경우 (입력 == fileinput 함수 [I]) \t \t \t found_string = TRUE; \t} \t if (! found_string) { \t \t updateLog (입력); \t} 오류 C2039 : 'Update_Click': 'My App :: Form1'의 구성원이 아닙니다 – user3740948

+0

보통 C++ 오류처럼 들리지 않습니다 ...이 "Update_Click"은 무엇입니까? – Cinch

관련 문제