Thread: Text files problem

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    Text files problem

    Dear All,
    I have some problems:
    As a commando line argument I get a path of the file which ending I have to replace.
    Now I would like to convert argv[1] argument into a string.
    When I read numbers from the text file how can I convert them into appropriate type double, int or float?
    I would also like to create a new text file in which I would like to write numbers in several collumns How can I control that all number columns are aligned?
    Thank you

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    std::string sArgv1 = argv[1];
    Perhaps?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by strickey View Post
    When I read numbers from the text file how can I convert them into appropriate type double, int or float?
    atoi, atof, atod. But then again, in C++, you can read directly to appropriate variable type, so you might not need conversion.

    Quote Originally Posted by strickey View Post
    I would also like to create a new text file in which I would like to write numbers in several collumns How can I control that all number columns are aligned?
    Thank you
    Try printing tab characters ("\t" I think?) in your file. Usually (in a good editor), the tabs will align to a specified position, so all the values, regardless of length will be aligned on that position the tab ends on.
    Otherwise just print one number per row.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    atoi, atof, atod. But then again, in C++, you can read directly to appropriate variable type, so you might not need conversion.



    Try printing tab characters ("\t" I think?) in your file. Usually (in a good editor), the tabs will align to a specified position, so all the values, regardless of length will be aligned on that position the tab ends on.
    Otherwise just print one number per row.
    Or using printing with correct number of digits, using printf format specifiers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by strickey View Post
    Dear All,
    I have some problems:
    As a commando line argument I get a path of the file which ending I have to replace.
    Commando line?


    Quote Originally Posted by strickey
    When I read numbers from the text file how can I convert them into appropriate type double, int or float?
    Depends on how you're reading through your file. If you have a float/double/int variable declared somewhere and you're reading through the file using the stream extraction operator (operator>>) then the value extracted will automatically be converted to the appropriate type for you... provided the value you are trying to extract can be converted.

    Quote Originally Posted by strickey
    I would also like to create a new text file in which I would like to write numbers in several collumns How can I control that all number columns are aligned?
    Thank you
    Use the stream formating member functions width/precision/fill/setf or the stream manipulators setw/setprecision/setfill/right/left found in the <iomanip> header to correctly align your columns.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. C: include files problem
    By threahdead in forum Linux Programming
    Replies: 4
    Last Post: 05-07-2004, 08:02 AM
  5. Problem with static text in dialog boxes
    By Clyde in forum Windows Programming
    Replies: 11
    Last Post: 05-28-2002, 12:51 PM