Thread: File I/O

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    File I/O

    Hi, I wanted to open a file for input and output. How do I change from this file:
    Code:
    :First line
    LINE_1 = foo
    :Second line
    LINE_2 = This is the second line.
    to

    Code:
    :First line
    LINE_1 = This is the first line.
    :Second line
    LINE_2 = This is the second line.
    I used this file for a config file. The lines with the first character of ':' is comments. The config itself can be changed in the application.

    The code I use to read the file is:
    Code:
      fstream filestr;
      char temp[256];
      char optName[256];
      char optValue[256];
      lstDataComp lstOptions; //typedef map<string,string> lstDataComp
      filestr.open ("./data/gamedata.txt", fstream::in| fstream::out);
      // Begin loading options
      
      filestr.getline(temp,256);
      while (filestr.good()) 
      {
      	while ((temp[0] == ':' || temp[0] == '\r' || temp[0] == '\0' || temp[0] == '\n' || strlen(temp) == 0)&&
    		filestr.good())
      	{
    		filestr.getline(temp,256);
      	}
    	if (filestr.good()) 
    	{
    		sscanf (temp, "%s = %s", 
    			optName, optValue);
    		lstOptions[string(optName)]=string(optValue);
    		filestr.getline(temp,256);
    	}
      }
      // End loading options
      filestr.close();
    The options were saved in a map container while the comments were ignored. But then I have to change the options in the application and save it again in the same config file. How do I do that without having to change the comments and other options' values???

    Thanks in advance...

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    If I understand you correctly, you want to have the one file change to
    Code:
    this is the first line
    this is the second line
    and that's it?

    In that case all you'd have to do is output into that file, no need to read it.

    Just do something like this
    Code:
    filestr << "This is the first line\n"; // or whatever you want the first line to be
    fliestr << "This is the second line\n"; // || for second.
    That seems overly simple in context to the code you posted though, so I may have misunderstood the question.

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by System_159
    If I understand you correctly, you want to have the one file change to
    Code:
    this is the first line
    this is the second line
    and that's it?

    In that case all you'd have to do is output into that file, no need to read it.

    Just do something like this
    Code:
    filestr << "This is the first line\n"; // or whatever you want the first line to be
    fliestr << "This is the second line\n"; // || for second.
    That seems overly simple in context to the code you posted though, so I may have misunderstood the question.

    Well, not exactly. You see, I just wanted to change a line while ignoring the others. The openmode itself is :"fstream::in| fstream:ut". Also, if I used your code, doesn't it will go like this:
    Code:
    this is the first line
    this is the second line
    this is the first line
    this is the second line
    And because I used the getline command, the get pointer will be at the end of file. So, any attempt to write a data will be done at the end of the file.

    EDIT: BTW, my code above actually for input and output. But because I still haven't figure out how to write the file, I only make the input part.
    Last edited by g4j31a5; 09-03-2006 at 10:55 PM.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You could read the whole file in to a variable, close the file, reopen the file (truncate the contents), and write the file back, line by line, replacing the lines you wanted to change.

    You might also consider using std::string instead of character arrays.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM