Solved

Reading and writing files from C

Posted on 2006-11-06
7
259 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);
    }

0
Comment
Question by:T3rminal
  • 3
  • 2
  • 2
7 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 17884972
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.
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17885330
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);
   
   
0
 

Author Comment

by:T3rminal
ID: 17885610
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)

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 16

Expert Comment

by:imladris
ID: 17885837
>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?
0
 
LVL 15

Accepted Solution

by:
bpmurray earned 500 total points
ID: 17885843
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);
   
0
 

Author Comment

by:T3rminal
ID: 17886092
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.
0
 

Author Comment

by:T3rminal
ID: 17887644
Fixed
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now