Reading lines from a .txt file

This is a discussion on Reading lines from a .txt file within the C Programming forums, part of the General Programming Boards category; Hi everyone! I really have no idea of how to program in C, but I use a piece of software ...

  1. #1
    Unregistered
    Guest

    Reading lines from a .txt file

    Hi everyone! I really have no idea of how to program in C, but I use a piece of software at work where every now and then I need to write a piece of code. Every time I have to write a piece of code I come to you guys so here I am again!
    I need the first line of data from a txt file and store it as a temporary value. This will then be written to another file but I have been able to work out how to do that part. I then need to loop round and pick up the second line of data. This wil go on and on until all 200 lines of data have been read from one file and then written to another. I know that it should onl be a few lines of relatively simple code but I have had no luck in solving this myself.

    Thanks in advance,

    Phil.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    34

    Post

    This code will copy the ENTIRE contents of a file. From here, it is a simple matter to copy only a given number of lines.

    Code:
    #include <stdio.h>
    
    static void MyCopyFile(char *FromFile, char *ToFile);
    
    int main(int argc, char* argv[])
    {
    	MyCopyFile("from.txt", "to.txt");
    
    	return 0;
    }
    
    static void
    MyCopyFile(char *FromFile, char *ToFile)
    {
    	FILE *from;
    	FILE *to;
    	char FileBuf[256];
    
    	if ((from = fopen(FromFile, "r")) != NULL)
    	{
    		/* THE "a" WILL APPEND TO THE FILE, IF YOU WANT TO CREATE A NEW FILE, USE "w" */
    		if ((to = fopen(ToFile, "a")) != NULL)
    		{
    			while (fgets(FileBuf, 255, from) != NULL)
    			{
    				fputs(FileBuf, to);
    			}
    			fclose(to);
    		}
    		else
    			printf("Cannot open %s\n", ToFile);
    		fclose(from);
    	}
    	else
    	{
    		printf("Cannot open %s\n", FromFile);
    	}
    }

  3. #3
    Unregistered
    Guest
    Thanks but it isn't that simple. I need to read the file line by line so that each line can then be broken-up, edited and then concacinated with other files. all very stupidly complex but i didn't write the software!! all i need to do is have a few lines of code to read a line and then put it into a variable, the rest i have already been able to do.

    Hope someone can help!

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    you could use the following...
    Code:
      while(fscanf(fp,"%[^\n]\n", string)!=EOF)
      {
          /*use a count variable (i) and work your way */
            /*thru the line using a pointer to the string */
           ptr = string;
           while(*ptr)
           {  /*do whatever*/
            
            }
      }
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  5. #5
    Unregistered
    Guest

    Thanks everyone

    Thankyou for all your help but I found that I was able to modify a command in the software that I use in order to bypass this whole situation.

    Thanks again.

    Phil.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. reading .txt file
    By vijay85 in forum C Programming
    Replies: 5
    Last Post: 03-09-2009, 05:07 PM
  3. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 09:59 PM
  4. Reading lines in from a file?
    By JarJarBinks in forum C++ Programming
    Replies: 8
    Last Post: 08-30-2004, 10:46 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21