Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem to write a text into file from the console input in c file program
please help me.
Posted
Comments
ridoy 19-Feb-13 2:50am    
what problem?you need to describe that..

The idea is to create a derivate of std::streambuf which will output data to both the file and cout. Then create an instance of it and use cout.rdbuf(...);

Try this (http://stackoverflow.com/a/3290886/1758762[^]):

C++
class StreambufDoubler : public std::streambuf {
public:
    StreambufDoubler(std::streambuf* buf1, std::streambuf* buf2) :
            _buf1(buf1), _buf2(buf2), _buffer(128)
    {
        assert(_buf1 && _buf2);

        setg(0, 0, 0);
        setp(_buffer.data(), _buffer.data(), _buffer.data() + _buffer.size());
    }

    ~StreambufDoubler() {
        sync();
    }

    void imbue(const std::locale& loc) {
        _buf1->pubimbue(loc);
        _buf2->pubimbue(loc);
    }

    std::streampos seekpos(std::streampos sp, std::ios_base::openmode which) {
        return seekoff(sp, std::ios_base::cur, which);
    }

    std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode which) {
        if (which | std::ios_base::in)
            throw(std::runtime_error("Can't use this class to read data"));

        // which one to return? good question
        // anyway seekpos and seekoff should never be called
        _buf1->pubseekoff(off, way, which);
        return _buf2->pubseekoff(off, way, which);
    }

    int overflow(int c) {
        int retValue = sync() ? EOF : 0;
        sputc(c);
        return retValue;
    }

    int sync() {
        _buf1->sputn(pbase(), pptr() - pbase());
        _buf2->sputn(pbase(), pptr() - pbase());
        setp(_buffer.data(), _buffer.data(), _buffer.data() + _buffer.size());
        return _buf1->pubsync() | _buf2->pubsync();
    }

private:
    std::streambuf*     _buf1;
    std::streambuf*     _buf2;

    std::vector<char>   _buffer;
};


int main() {
    std::ofstream myFile("file.txt");
    StreambufDoubler doubler(std::cout.rdbuf(), myFile.rdbuf());
    std::cout.rdbuf(&doubler);

    // your code here

    return 0;
}
</char>
 
Share this answer
 
v2
Comments
Herambashree 19-Feb-13 5:42am    
I asked you for the code in C.
Leo Chapiro 19-Feb-13 6:02am    
I added an example for C in solution 2
C#
Here is an example (please run the executable from the command line):

#include <stdio.h>
#include <string.h>

int main()
{
  FILE *file;
  int i;
  char firstName[32];
  char lastName[32];
  int found = 0;

  // Open the file for writing
  file = fopen("records.txt", "wt");
  if (!file)
  {
    printf("File could not be opened\n\a\a");
    getchar();
    return -1;
  }

  // Read and save data
  for (i = 0; i < 3; ++i)
  {
    // Read data
    printf("Record #%d\n", i + 1);
    printf("Enter first name: "); scanf("%s", firstName);
    printf("Enter last name:  "); scanf("%s", lastName);
    printf("\n");

    // Save data
    fprintf(file, "%s\t%s\n", firstName, lastName);
  }

  // Close the file
  fclose(file);

  // Open the file for reading
  file = fopen("records.txt", "rt");
  if (!file)
  {
    printf("File could not be opened\n\a\a");
    return -1;
  }

  // Load and display data
  i = 0;
  while(!feof(file) && !found)
  {
    ++i;
    fscanf(file, "%s\t%s", firstName, lastName);
    if (strcmp(firstName, "John") == 0 && strcmp(lastName, "Doe") == 0)
    {
      printf("Record found (#%d): %s %s\n", i, firstName, lastName);
      found = 1;
    }
  }
  if (!found)
    printf("Record could not be found");

  // Close the file
  fclose(file);

  return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900