Thread: Open, search, act, close.

  1. #1
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    Open, search, act, close.

    Ok heres the idea, i have two text files. I want the program to open them both, check for a match from the users input in the first file, if its found i want it to go to the matching id number in the second file, then output the entire line from the second file. All i know is i need to us if.stream()??

    Example:

    Both files opened.

    User enters: Hi how are you today.

    Program scans first file for hi, how, are, you, and today.

    it finds this line:

    2 how,are,you

    it then searches the second file for the line

    2 I am fine, thank you for asking.

    and outputs: I am fine, thank you for asking.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    You will use functions including open(), read() or getline().

    You already have the algorithm laid out. Just implement it.

    Kuphryn

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    ok, where might i find the syntax for these? I don't want to sound lazy, i'd look but i don't really know WHAT i'm looking for, as i've never done this before.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Ok, I am going to chastise you a bit here. You have almost 700 posts here and ask these sorts of questions? You have to be diligent. Program day and night, trying everything you can possibly think of - don't let up or relax one bit! You must apply yourself constantly.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Yea, he just spams in General Discussions...

  6. #6
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Well i tried

    http://www.cprogramming.com/tutorial/lesson10.html

    but it has all kinds of errors. Its annoying to have absolutely no idea how to opena nd close files >_<
    Last edited by RoD; 10-13-2002 at 01:01 PM.

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Post the errors

  8. #8
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Let me first say that the filepath is not in " " because when it is it tells me \k (part of the path) is not a valid escape sequence.


    PHP Code:
    --------------------Configurationtest Win32 Debug--------------------
    Compiling...
    test.cpp
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(15) : error C2065'c' undeclared identifier
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(15) : error C2143syntax error missing ')' before ':'
    C:\Documents and Settings\NEKEY\Desktop\test.cpp(15) : error C2448'<Unknown>' : function-style initializer appears to be a function definition
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(15) : error C2017illegal escape sequence
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(15) : error C2059syntax error ')'
    C:\Documents and Settings\NEKEY\Desktop\test.cpp(19) : error C2065'a_file' undeclared identifier
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(19) : error C2297'<<' illegalright operand has type 'char [45]'
    C:\Documents and Settings\NEKEY\Desktop\test.cpp(23) : error C2228left of '.close' must have class/struct/union type
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(27) : warning C4129'k' unrecognized character escape sequence
    Error executing cl
    .exe.

    test.obj 8 error(s), 1 warning(s


    Code:
    #include <fstream.h>
    
    #include <iostream.h>
    
    
    
    int main()
    
    {
    
      char str[10];  
    
             //Used later
    
      ofstream a_file(c:\keywords.txt);
    
    			//Creates an instance of ofstream, and opens example.txt
    
      a_file<<"This text will now be inside of keywords.txt"; 
    
    			//Outputs to example.txt through a_file
    
      a_file.close();                           
    
    			//Closes up the file
    
      ifstream b_file(c:\keywords.txt); 
    
    			//Opens for reading the file
    
      b_file>>str;    
    
                 //Reads one string from the file
    
      cout<<str;   
    
               
    
      b_file.close();    
    
               //Do not forget this! 
    
      return 0;
    
    }

  9. #9
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    ofstream a_file(c:\keywords.txt);

    Uhhm, change that to a string.
    Also, you need to use double front slashes, or use a single back slash.
    ofstream a_file("c:\\keywords.txt");

  10. #10
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Uhhm, change that to a string.
    This is going to sound like a rlly dumb question, but what do u mean.

  11. #11
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    What is c:\keywords.txt? What type of variable is it?

  12. #12
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    The output is odd seeing as how it resembles nothing in the keywords.txt file...

    Output:

    ╠╠╠╠╠╠╠╠╠╠╠╠░_↕Press any key to continue

    Code:
    #include <fstream.h>
    
    #include <iostream.h>
    
    
    
    int main()
    
    {
    
      char str[10];  
    
             //Used later
    
      ofstream a_file("c:\\keywords.txt");
    
    			//Creates an instance of ofstream, and opens example.txt
    
      
    
    		
    
      a_file.close();                           
    
    			//Closes up the file
    
       ifstream b_file("c:\\keywords.txt"); 
    
    			//Opens for reading the file
    
      b_file>>str;    
    
                 //Reads one string from the file
    
      cout<<str;   
    
               //Should output 'this'
    
      b_file.close();    
    
               //Do not forget this! 
    
      return 0;
    
    }
    Last edited by RoD; 10-13-2002 at 03:13 PM.

  13. #13
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Code:
    #include <fstream.h>
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    
      char str[10];  
      ofstream a_file;
      a_file.open("C:\keywords.txt");
      //you don't do anything here, why even have an a_file? Oh well..
      a_file.close();                           	
       ifstream b_file;
      b_file.open("C:\keywords.txt");
      b_file.getline(str, 10, '\n');//reads 10 characters from a file, or until a line break.
    //this method reads spaces too, yours doesn't...
      cout<<str;   
      b_file.close();        
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Performance issue!
    By maven in forum C Programming
    Replies: 42
    Last Post: 03-23-2009, 11:57 AM
  2. Firefox and Google Search
    By DeepFyre in forum Tech Board
    Replies: 0
    Last Post: 01-16-2005, 10:28 AM
  3. Simple search program
    By colinuk in forum C Programming
    Replies: 6
    Last Post: 12-18-2004, 01:58 AM
  4. Replies: 8
    Last Post: 11-21-2001, 12:13 AM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM