How to copy a word or numbers from text to other location

This is a discussion on How to copy a word or numbers from text to other location within the C Programming forums, part of the General Programming Boards category; HI I would like to open a file with text and numbers and copy from the text few words and ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    23

    How to copy a word or numbers from text to other location

    HI
    I would like to open a file with text and numbers and copy from the text few words and numbers.
    Like if you want some data from system and want to data from system location be stored into another location.
    Example like if you want data of used memory(that is allready written in a file) to be stored in some other location, but in the file where numerical data is, there are also a few others numbers and I want just one to be copied to string.
    I've managed to open file and know how to do to display all data in the file(EOF), but to pick only one number or word from text and display it in other program, that's a little bit too much for me. Can anyone help me?
    Have a nice Day

    Matt

    I copied that from someone but here is only all file display
    Code:
    #include <stdio.h>
    
    main()
    {
      char data[80];		/* Record read from the file.		*/
      FILE *ptr;			/* Pointer to the file. FILE is a 
    				   structure  defined in <stdio.h>	*/
    
    				/* Open the file - no error checking done */
      ptr = fopen("/proc/meminfo","r");
    				/* Read one record at a time, checking 
    				   for the End of File. EOF is defined 
    				   in <stdio.h>  as -1 			*/
    
      while (feof(ptr) == 0)
      {
        fgets(data, 80, ptr);	/* Read next record			*/
        if (data[0] == '#') continue; /* filter out the comments		*/
        printf("%s",data);	/* O/P the record to the screen		*/
      }
    
      fclose(ptr);			/* Close the file.			*/
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    1.Open the file you want to copy stuff from.

    2.Search for the desired text using strcmp.

    3.Write it into the new file.
    Thanks,
    Sahil

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    23
    What if I would want a number that is always changing.I just want how to determine location of nubers/text, so the it will always display anything on that location. I can't type something I don't know what will appear there. Amount of used and free memory is always changing so I must get the amount, but don't know how to do that, but I do know in which line it is and if its the first number or seconf or third in the line.
    Have a nice Day

    Matt

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    23
    Nobody can help me at the moment?
    Have a nice Day

    Matt

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    You gave up too early, and you didn't post your latest code.

    You already know how to filter out comments, just add some more if() statements to recognise more examples which interest you.

    Say strcmp() and strstr() perhaps.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    23
    HI
    I was thinking a bit longer and I realized that my concept of getting the values of free memory isn't the right one. Does anyone have any experience with function getfreemem()?
    I haven't got a clue what to write into the brackets.

    Have a nice Day

    Matt

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Most people start with
    man getfreemem

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    23
    No manual entry for getfreemem

    Obviously no function available or what.
    Have a nice Day

    Matt

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Well if you don't have the function, you're going to have to use another approach.
    How about persevering with your original approach then?

    There's only the vaguest of hints that you're on a "Unix" platform of some sort, but I wouldn't be surprised if you said you were on win32 (you would not be the first to take a random bit of unix code and try and use it unmodified on windows).
    So how about stating your actual OS/Compiler, then perhaps we can suggest functions to use.


    If it's not too much, paste the result of
    cat /proc/meminfo
    to the board, and highlight the field(s) you want to extract.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    23
    total: used: free: shared: buffers: cached:
    Mem: 516644864 27402240 489242624 0 2265088 16936960
    Swap: 0 0 0
    MemTotal: 504536 kB
    MemFree: 477776 kB
    MemShared: 0 kB
    Buffers: 2212 kB
    Cached: 16540 kB
    SwapCached: 0 kB
    Active: 6988 kB
    Inactive: 12784 kB
    HighTotal: 0 kB
    HighFree: 0 kB
    LowTotal: 504536 kB
    LowFree: 477776 kB
    SwapTotal: 0 kB
    SwapFree: 0 kB
    Committed_AS: 1160 kB
    VmallocTotal: 491512 kB
    VmallocUsed: 596 kB
    VmallocChunk: 490916 kB

    This is meminfo at the moment - I want to save somewhere number at MemFree(MemFree: 477776 kB)
    so I need just a number. I have Monta Vista Linux operating system
    I want that number then 1000 times smaller(in MB) and then that memtester searches for this number how mouch memory to check.

    The program I pasted before is copied, just path for opening file is written by me

    Have a nice Day

    Matt

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Something like
    Code:
    while ( fgets(data, sizeof(data), ptr) != NULL ) {
      unsigned long memSize;
      char memScale[10];
      if ( sscanf( data, "MemFree: %lu %9s", &memSize, memScale ) == 2 ) {
        // memSize contains the size
        // memScale contains "kb"
      }
    }
    > then that memtester searches for this number how mouch memory to check.
    You're trying to write a memory tester?

    How do you propose to access real memory when all Linux will give you is virtual memory for your programs.

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    23
    HI
    Memtester is allready written, I just have to add a part that you don't have to type amount of memory to check, but memtester checks all free memory himself, and you have to put memory size in MB
    Have a nice Day

    Matt

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    23
    HI
    I found out that meminfo doesn't display real data. Free -m does.
    First I run function free -m to see how much free memory available and then I put the exact number into memtester and memtester did the test. When I entered 1MB higher number(previous was 987. next 988) and the memtester killed process - out of memory. So now I know that the max available free memory is the exact number from free function.
    So how can I save that number from that function(how can I actually get to free function). Everything else I can do myself
    Have a nice Day

    Matt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. stripping text from a word doc
    By cakewalkr7 in forum C Programming
    Replies: 1
    Last Post: 05-09-2002, 07:45 AM
  4. Using text in MS Word
    By nate11000 in forum Windows Programming
    Replies: 2
    Last Post: 03-14-2002, 03:25 PM
  5. Replies: 3
    Last Post: 02-08-2002, 09:15 PM

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