[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Reading and writing files from C

Posted on 2006-11-06
7
Medium Priority
?
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 nested-loops in the C programming language.
Suggested Courses

649 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