Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. read file from bottom

read file from bottom

Scheduled Pinned Locked Moved The Weird and The Wonderful
helpquestion
20 Posts 16 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V vineeshV

    I need to read a file from the bottom is it possible in c language ? The exact requirement is suppose having a file like ======================================== hi how are you exit 1 then you exit 1 and i can make this exit 0 =========================================== I need to read the file from bottom to search the last exit 1 and print the lines above it the output will be then you Can anybody help me ??

    vineesh

    L Offline
    L Offline
    Lee Humphries
    wrote on last edited by
    #2

    Yep that's a coding horror. And by the way of course you can do that in C - try poking around in stdio.h and reading a book or two. Maybe use Google.

    I am convinced that lobotomising users will make little to no difference.

    K 1 Reply Last reply
    0
    • L Lee Humphries

      Yep that's a coding horror. And by the way of course you can do that in C - try poking around in stdio.h and reading a book or two. Maybe use Google.

      I am convinced that lobotomising users will make little to no difference.

      K Offline
      K Offline
      KarstenK
      wrote on last edited by
      #3

      Thats a very special kind of humour. :-O

      Greetings from Germany

      1 Reply Last reply
      0
      • V vineeshV

        I need to read a file from the bottom is it possible in c language ? The exact requirement is suppose having a file like ======================================== hi how are you exit 1 then you exit 1 and i can make this exit 0 =========================================== I need to read the file from bottom to search the last exit 1 and print the lines above it the output will be then you Can anybody help me ??

        vineesh

        B Offline
        B Offline
        Baconbutty
        wrote on last edited by
        #4

        Yes. R. T. F. M.

        I still remember having to write your own code in FORTRAN rather than be a cut and paste merchant being pampered by colour coded Intellisense - ahh proper programming - those were the days :)

        M 1 Reply Last reply
        0
        • V vineeshV

          I need to read a file from the bottom is it possible in c language ? The exact requirement is suppose having a file like ======================================== hi how are you exit 1 then you exit 1 and i can make this exit 0 =========================================== I need to read the file from bottom to search the last exit 1 and print the lines above it the output will be then you Can anybody help me ??

          vineesh

          K Offline
          K Offline
          killabyte
          wrote on last edited by
          #5

          #include "stdafx.h" #include "stdio.h" #include "stdlib.h" #include "string.h" #define MEANINGFULL_SIZE 256 #define DELIM "\nexit 1\n" #define FILE_DATA "hi\nhow are you\nexit 1\nthen\nyou\nexit 1\nand i can\nmake this\nexit 0\n" int _tmain(int argc, _TCHAR* argv[]) { char* filestr = (char*)malloc(MEANINGFULL_SIZE); char* Found = (char*)malloc(MEANINGFULL_SIZE); char* Current = {0}; memcpy(filestr,FILE_DATA,MEANINGFULL_SIZE-1); //pseudo readfile Current = strstr(filestr,DELIM); //Find Stuff memcpy(Found,Current+strlen(DELIM),strlen(Current));//copy what ever was found minus the delims Current = strstr(Found,DELIM);//find the next delims *Current = '\0'; //truncate the string printf("%s \n",Found);//publish the result while(1); return 0; } Hope this helps u ;P

          modified on Tuesday, July 1, 2008 5:57 AM

          J Y S R 4 Replies Last reply
          0
          • B Baconbutty

            Yes. R. T. F. M.

            I still remember having to write your own code in FORTRAN rather than be a cut and paste merchant being pampered by colour coded Intellisense - ahh proper programming - those were the days :)

            M Offline
            M Offline
            Mladen Jankovic
            wrote on last edited by
            #6

            No, you have it wrong. He said he needed to read the file from the bottom: M. F. T. R.

            Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

            C 1 Reply Last reply
            0
            • K killabyte

              #include "stdafx.h" #include "stdio.h" #include "stdlib.h" #include "string.h" #define MEANINGFULL_SIZE 256 #define DELIM "\nexit 1\n" #define FILE_DATA "hi\nhow are you\nexit 1\nthen\nyou\nexit 1\nand i can\nmake this\nexit 0\n" int _tmain(int argc, _TCHAR* argv[]) { char* filestr = (char*)malloc(MEANINGFULL_SIZE); char* Found = (char*)malloc(MEANINGFULL_SIZE); char* Current = {0}; memcpy(filestr,FILE_DATA,MEANINGFULL_SIZE-1); //pseudo readfile Current = strstr(filestr,DELIM); //Find Stuff memcpy(Found,Current+strlen(DELIM),strlen(Current));//copy what ever was found minus the delims Current = strstr(Found,DELIM);//find the next delims *Current = '\0'; //truncate the string printf("%s \n",Found);//publish the result while(1); return 0; } Hope this helps u ;P

              modified on Tuesday, July 1, 2008 5:57 AM

              J Offline
              J Offline
              jamie550
              wrote on last edited by
              #7

              :laugh:

              1 Reply Last reply
              0
              • M Mladen Jankovic

                No, you have it wrong. He said he needed to read the file from the bottom: M. F. T. R.

                Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #8

                :-D

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                1 Reply Last reply
                0
                • K killabyte

                  #include "stdafx.h" #include "stdio.h" #include "stdlib.h" #include "string.h" #define MEANINGFULL_SIZE 256 #define DELIM "\nexit 1\n" #define FILE_DATA "hi\nhow are you\nexit 1\nthen\nyou\nexit 1\nand i can\nmake this\nexit 0\n" int _tmain(int argc, _TCHAR* argv[]) { char* filestr = (char*)malloc(MEANINGFULL_SIZE); char* Found = (char*)malloc(MEANINGFULL_SIZE); char* Current = {0}; memcpy(filestr,FILE_DATA,MEANINGFULL_SIZE-1); //pseudo readfile Current = strstr(filestr,DELIM); //Find Stuff memcpy(Found,Current+strlen(DELIM),strlen(Current));//copy what ever was found minus the delims Current = strstr(Found,DELIM);//find the next delims *Current = '\0'; //truncate the string printf("%s \n",Found);//publish the result while(1); return 0; } Hope this helps u ;P

                  modified on Tuesday, July 1, 2008 5:57 AM

                  Y Offline
                  Y Offline
                  Yusuf
                  wrote on last edited by
                  #9

                  evil :laugh:

                  Yusuf

                  1 Reply Last reply
                  0
                  • K killabyte

                    #include "stdafx.h" #include "stdio.h" #include "stdlib.h" #include "string.h" #define MEANINGFULL_SIZE 256 #define DELIM "\nexit 1\n" #define FILE_DATA "hi\nhow are you\nexit 1\nthen\nyou\nexit 1\nand i can\nmake this\nexit 0\n" int _tmain(int argc, _TCHAR* argv[]) { char* filestr = (char*)malloc(MEANINGFULL_SIZE); char* Found = (char*)malloc(MEANINGFULL_SIZE); char* Current = {0}; memcpy(filestr,FILE_DATA,MEANINGFULL_SIZE-1); //pseudo readfile Current = strstr(filestr,DELIM); //Find Stuff memcpy(Found,Current+strlen(DELIM),strlen(Current));//copy what ever was found minus the delims Current = strstr(Found,DELIM);//find the next delims *Current = '\0'; //truncate the string printf("%s \n",Found);//publish the result while(1); return 0; } Hope this helps u ;P

                    modified on Tuesday, July 1, 2008 5:57 AM

                    S Offline
                    S Offline
                    Stephen Hewitt
                    wrote on last edited by
                    #10

                    Standard header files should be included withh < and >. eg. #include <stdio.h>.

                    Steve

                    K 1 Reply Last reply
                    0
                    • V vineeshV

                      I need to read a file from the bottom is it possible in c language ? The exact requirement is suppose having a file like ======================================== hi how are you exit 1 then you exit 1 and i can make this exit 0 =========================================== I need to read the file from bottom to search the last exit 1 and print the lines above it the output will be then you Can anybody help me ??

                      vineesh

                      P Offline
                      P Offline
                      Paul Conrad
                      wrote on last edited by
                      #11

                      vineeshV wrote:

                      Can anybody help me ?

                      Sure....how about picking the correct forum :|

                      "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                      1 Reply Last reply
                      0
                      • S Stephen Hewitt

                        Standard header files should be included withh < and >. eg. #include <stdio.h>.

                        Steve

                        K Offline
                        K Offline
                        killabyte
                        wrote on last edited by
                        #12

                        Doesnt that just change the search scope for the file included? which will have little effect if it is increased scope maybe u wil lose 0.00000000001n.S in compile time. (realising that 85% of stats are made up on the spot) if it does have some esoteric effect that i have been lucky enough to avoid thus far please educate me.

                        S 1 Reply Last reply
                        0
                        • K killabyte

                          Doesnt that just change the search scope for the file included? which will have little effect if it is increased scope maybe u wil lose 0.00000000001n.S in compile time. (realising that 85% of stats are made up on the spot) if it does have some esoteric effect that i have been lucky enough to avoid thus far please educate me.

                          S Offline
                          S Offline
                          Stephen Hewitt
                          wrote on last edited by
                          #13

                          First off, it's good style. Secondly it effects the search paths used.

                          Steve

                          1 Reply Last reply
                          0
                          • V vineeshV

                            I need to read a file from the bottom is it possible in c language ? The exact requirement is suppose having a file like ======================================== hi how are you exit 1 then you exit 1 and i can make this exit 0 =========================================== I need to read the file from bottom to search the last exit 1 and print the lines above it the output will be then you Can anybody help me ??

                            vineesh

                            E Offline
                            E Offline
                            Edward Steward
                            wrote on last edited by
                            #14

                            At the risk of sounding stupid, why don't you do it two phases: 1. Read the file into a datatable, adding an additional column to represent a line number 2. Sort the table by line number (desc) and loop through. Edward

                            Edward Steward edwardsteward@optusnet.com.au

                            D 1 Reply Last reply
                            0
                            • V vineeshV

                              I need to read a file from the bottom is it possible in c language ? The exact requirement is suppose having a file like ======================================== hi how are you exit 1 then you exit 1 and i can make this exit 0 =========================================== I need to read the file from bottom to search the last exit 1 and print the lines above it the output will be then you Can anybody help me ??

                              vineesh

                              P Offline
                              P Offline
                              Punithkumar
                              wrote on last edited by
                              #15

                              //initialize streamreader StreamReader myStream = new StreamReader(@"c:\myfile.txt"); //read all the contents and store in string string WholeFileString=myStream.ReadToEnd(); //convert to array char []strArray = WholeFileString.ToCharArray(); //reverse the array Array.Reverse(strArray); //again convert to string string newStr = new string(strArray); //specify the split array (Note \r\n must be specified reverse becoz string is reverse char [] split = new char[2]; split[0] = '\n'; split[1] = '\r'; //receive the string array after spliting string [] Lines = newStr.Split(split); lines[0] = ; //last line of the file lines[lines.Length-1] = ; //first line of the file

                              Regards, Punithkumar

                              S 1 Reply Last reply
                              0
                              • P Punithkumar

                                //initialize streamreader StreamReader myStream = new StreamReader(@"c:\myfile.txt"); //read all the contents and store in string string WholeFileString=myStream.ReadToEnd(); //convert to array char []strArray = WholeFileString.ToCharArray(); //reverse the array Array.Reverse(strArray); //again convert to string string newStr = new string(strArray); //specify the split array (Note \r\n must be specified reverse becoz string is reverse char [] split = new char[2]; split[0] = '\n'; split[1] = '\r'; //receive the string array after spliting string [] Lines = newStr.Split(split); lines[0] = ; //last line of the file lines[lines.Length-1] = ; //first line of the file

                                Regards, Punithkumar

                                S Offline
                                S Offline
                                StevenWalsh
                                wrote on last edited by
                                #16

                                C# is the new C i see

                                K 1 Reply Last reply
                                0
                                • S StevenWalsh

                                  C# is the new C i see

                                  K Offline
                                  K Offline
                                  killabyte
                                  wrote on last edited by
                                  #17

                                  #define TRUE (C > C#) :-D

                                  1 Reply Last reply
                                  0
                                  • K killabyte

                                    #include "stdafx.h" #include "stdio.h" #include "stdlib.h" #include "string.h" #define MEANINGFULL_SIZE 256 #define DELIM "\nexit 1\n" #define FILE_DATA "hi\nhow are you\nexit 1\nthen\nyou\nexit 1\nand i can\nmake this\nexit 0\n" int _tmain(int argc, _TCHAR* argv[]) { char* filestr = (char*)malloc(MEANINGFULL_SIZE); char* Found = (char*)malloc(MEANINGFULL_SIZE); char* Current = {0}; memcpy(filestr,FILE_DATA,MEANINGFULL_SIZE-1); //pseudo readfile Current = strstr(filestr,DELIM); //Find Stuff memcpy(Found,Current+strlen(DELIM),strlen(Current));//copy what ever was found minus the delims Current = strstr(Found,DELIM);//find the next delims *Current = '\0'; //truncate the string printf("%s \n",Found);//publish the result while(1); return 0; } Hope this helps u ;P

                                    modified on Tuesday, July 1, 2008 5:57 AM

                                    R Offline
                                    R Offline
                                    Rage
                                    wrote on last edited by
                                    #18

                                    killabyte wrote:

                                    char* filestr = (char*)malloc(MEANINGFULL_SIZE);

                                    Shouldn't soemthing be freed somewhere ?

                                    K 1 Reply Last reply
                                    0
                                    • R Rage

                                      killabyte wrote:

                                      char* filestr = (char*)malloc(MEANINGFULL_SIZE);

                                      Shouldn't soemthing be freed somewhere ?

                                      K Offline
                                      K Offline
                                      killabyte
                                      wrote on last edited by
                                      #19

                                      :-D yes in a perfect world

                                      modified on Friday, July 11, 2008 9:16 PM

                                      1 Reply Last reply
                                      0
                                      • E Edward Steward

                                        At the risk of sounding stupid, why don't you do it two phases: 1. Read the file into a datatable, adding an additional column to represent a line number 2. Sort the table by line number (desc) and loop through. Edward

                                        Edward Steward edwardsteward@optusnet.com.au

                                        D Offline
                                        D Offline
                                        dybs
                                        wrote on last edited by
                                        #20

                                        Just read the file into an array of lines, and loop backwards to the first occurrence of the delimiter (exit 1), keep track of the index you found and go from there. Dybs

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups