[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

Reading and writing files from C

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
T3rminal
Asked:
T3rminal
  • 3
  • 2
  • 2
1 Solution
 
imladrisCommented:
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
 
bpmurrayCommented:
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
 
T3rminalAuthor 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)

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
imladrisCommented:
>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
 
bpmurrayCommented:
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
 
T3rminalAuthor 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.
0
 
T3rminalAuthor Commented:
Fixed
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now