Solved

Reading and writing files from C

Posted on 2006-11-06
7
239 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

758 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

27 Experts available now in Live!

Get 1:1 Help Now