Link to home
Start Free TrialLog in
Avatar of malaou
malaou

asked on

How to insert a text string into an existing text file in C?

I am trying to open a text_file1.txt and insert a text string "***This is a test ***" into the 3rd line of the file.  Please help.



Avatar of randis
randis

2 ways.
1 always works and the other depends on memory and os.

method 1:
copy the file to a temp file.  Rewrite the file over the original, inserting the string where you want.

method 2: read the entire file into memory.  Rewrite the original file, inserting the string when you want to.
/*You can try something like this:*/
int main()
{
  FILE *in, *out;
  char buf[256];
  int line=0;

  if ((in = fopen("in.txt", "rt")) == NULL)
  {
    puts("Unable to open the file");
    return 0;
  }

  out = fopen("copy.tmp", "wt");

  while (!feof(in))
  {
    fgets(buf,255,in);
    fputs(buf, out);
  }

  fclose(in);
  fclose(out);

  if ((in = fopen("copy.tmp", "rt")) == NULL)
  {
    puts("Unable to open the file");
    return 0;
  }

  out = fopen("in.txt", "wt");

  while (!feof(in))
  {
    fgets(buf,255,in);
    line++;
    if(line==3)
      fputs("***This is a test ***",out);
    fputs(buf, out);
  }

  fclose(in);
  fclose(out);

  return 0;
}
ASKER CERTIFIED SOLUTION
Avatar of ewest
ewest

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of malaou

ASKER

Also, How can I write the final output to a variable, so it can be used later in the program?
There are many ways.  They range from simple to advanced.
The choice must depend on your knowledge of the programming language, the platform you are working with as well as the file type.

For example, if the text file is a Webster Dictionary, don't keep it in memory.  Simply re-open it again whenever you want to do something else to it.  This is probably the simplest most powerful (no limit on file size) solution.

if you are using an advanced form of the language (C++
 Buider, for example), you may have code that already holds files.  (TStrings->LoadFromFile(...) for example)).

If your language includes the STL (standard Template Library) which includes most compilers after 1996, and you can use them, you can use one of their data structures (such as linked list) to hold your file (char arrays).

Finaly, if you know your file will be short, you can use an array to hold them. two examples:
1. char file[50][256]; // max 50 lines, 255 chars per line max.
2. char *file[50]; // max 50 lines, very long lines possible (depends on OS memory model), but pointers required.


Good luck.
There are many ways.  They range from simple to advanced.
The choice must depend on your knowledge of the programming language, the platform you are working with as well as the file type.

For example, if the text file is a Webster Dictionary, don't keep it in memory.  Simply re-open it again whenever you want to do something else to it.  This is probably the simplest most powerful (no limit on file size) solution.

if you are using an advanced form of the language (C++
 Buider, for example), you may have code that already holds files.  (TStrings->LoadFromFile(...) for example)).

If your language includes the STL (standard Template Library) which includes most compilers after 1996, and you can use them, you can use one of their data structures (such as linked list) to hold your file (char arrays).

Finaly, if you know your file will be short, you can use an array to hold them. two examples:
1. char file[50][256]; // max 50 lines, 255 chars per line max.
2. char *file[50]; // max 50 lines, very long lines possible (depends on OS memory model), but pointers required.


Good luck.
Avatar of malaou

ASKER

Thanks for your helps.