We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Reading and writing files from C

T3rminal
T3rminal asked
on
Medium Priority
300 Views
Last Modified: 2010-04-15
Hi,

I am working with reading and writing files of text in C.  Currently the below code will read one line of text fine but as soon as I add another it will then only take the last line of text into the string - or that is what is printed, at least - how can this be fixed to take all lines of text from the file?  And after putting all the text into a string so it can modified, I'd then like to dump that string to a new text file.


   char str[255];

   FILE * src = fopen("c:\\data.txt", "r");

    while (!feof(src))  {
          fgets(str, sizeof(str), src);
    }

Comment
Watch Question

Commented:
HMMmmm. A C program wouldn't normally read a whole file into a string. If that is needed, for some reason, it would usually be done with dynamic memory allocation. That is, the program would somehow ascertain how much memory was needed to hold the file, allocate that, then read the file into it, and do the requisite processing.

However, if you are dealing only with simpler static memory allocation, it is usually much easier and more efficient to operate on a file one line at a time. In psuedo code, in order to read and print a file, and copy it to another it would look something like:

char line[255];

open file
while read a line succeeds
    print it
    write it to another file


This prints the file line by line and copies it line by line to another file, thus accomplishing the purpose without all the memory requirements of reading the whole thing first.

Commented:
You can read the entire file doing something like this:

   char *str;
   long  fsize;

   FILE * src = fopen("c:\\data.txt", "r");

   fseek( src, 0, SEEK_END);
   fsize = ftell(src);

   str = malloc(fsize+1);
   fread(str, 1, fsize, src);
   
   

Author

Commented:
Hmmm, I cant get that solution to work, still only seems to take the last line and it is displayed as symbols.  Reading line by line also does not seem an option as I need to work (split it into words) with the string before I dump it.

Using the below, I can get a full print out of the text but once I actually say copy the string or use strtok, it will just takes the bottom line.  Is there some way to parse the string of new lines in a text file before I copy it or perhaps reading it line by line adding each line to the string?

char str[255]

    if (!src)
        return 1;
    while (fgets(str,sizeof(str),src)!=NULL)

Commented:
>Reading line by line also does not seem an option as I need to work (split it into words) with the string before I dump it.

I don't know all the details of what you need to do. However, it sounds like you should be able to read the line, print it, split the line into words, dump those to a file, and then process the next line.

Wouldn't that work?
If not, why not?
Commented:
Sorry - I forgot to reset the pointer ....

char *str;
   long  fsize;

   FILE * src = fopen("c:\\data.txt", "r");

   fseek( src, 0, SEEK_END);
   fsize = ftell(src);
   fseek(src, 0, SEEK_SET);  // Rewind the file to the start

   str = malloc(fsize+1);
   fread(str, 1, fsize, src);
   

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks bpmurray, that is working well but there is some trash getting added to the string, any way to clean that up? et 2003\sdkn and such, i guess it is from the malloc on str.

imladris, I need access to work with all the words from the text as one, not line per line, the dumping would come after that is done.

Author

Commented:
Fixed
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.