Thread: Linked to MSCVRT, deleting first line of a file.

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    Linked to MSCVRT, deleting first line of a file.

    Hi I'm trying to make a program that deletes the first line of a file, but when I compile it the exe is dinamically linked to MSVCR100.dll (I'm using Visual Studio 2010, but I guess it would be MSVCRTX.dll being X the version, for example MSVCRT90.dll if it was VS 2008).

    I would like to ask 3 things:
    Is there any way to compile and don't depend on MSVCRT (or depend on MSVCRT.dll, I mean the one that comes with Windows)?
    The "main" syntax, can it be easier or do I have to stick with it? Which one is the standard?

    And finally, is there any better way to implement it?

    Thanks in advance.

    Code:
    #include <string>
    #include <fstream>
    #include <iostream>
    #include <tchar.h>
    
    int _tmain(int argc, _TCHAR* argv[]) {
       std::string line;
       std::ifstream in ("Files");
       std::ofstream out ("Files.tmp");
    
       if (!in.is_open ()) {
          std::cout << "Input file failed to open.\n";
          return 1;
       }
    
       getline (in, line);
       while (getline (in, line)) {
          out << line << "\n";
       }
       in.close ();
       out.close ();
    
       remove ("Files");
       rename ("Files.tmp", "Files");
       return 0;
    }

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Link to the c run time statically. In visual studio 2008 Project Settings->c/c++->Code Generation.

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by Francisco View Post
    (or depend on MSVCRT.dll, I mean the one that comes with Windows)?
    Not with Visual Studio, the import library for it is in the WDK. It isn't recommended anyway, the msvcrt.dll that comes with XP+ is really msvc70.dll, the Visual Studio 2002 CRT and is missing a few of the functions that programs compiled with later versions of VS use.

    The "main" syntax, can it be easier or do I have to stick with it? Which one is the standard?
    The standard ones are:
    int main()
    or
    int main(int argc, char** argv)

    If you don't know what a _TCHAR is or what it allows then chances are you don't need it. It won't affect you until you actually use argv at which point you'll probably get loads of errors like this one. If you're just beginning, use the second version above. It'll make you're life easier in regards to tutorials/books etc, and let your code work on other compilers too.

    And finally, is there any better way to implement it?
    You could do without the temp file if you kept the file contents in memory and reopened the input file as an output file (use an ofstream with std::ios:trunc) and wrote to that. If you give it a go, it'll provide an opportunity to get familiar with vectors, which are really quite useful.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    Wink Thanks for everything!

    Thanks for the replies!

    I'll be linking the CRT statically for this simple program and I've taken a look at vectors and what you proposed. This way there are less I/O operations .

    I've changed the main to one of the standard ones (and make use of argv[] to make the program reusable, the one before was the default one when you select a console app in VS 2010 (Beta 1). I've looked at it too, and for the records I'll post what I've founded (by default the standard syntax didn't produce an executable but a linker error):

    In release configurations:
    Code:
    LIBCMT.lib(wcrt0.obj) : error LNK2001: unresolved external symbol _wmain
    In debug configurations:
    Code:
    MSVCRTD.lib(wcrtexe.obj) : error LNK2019: unresolved external symbol _wmain referenced in function ___tmainCRTStartup
    Cause (somebody wrote this as a response for an already open "bug" ticket for VS 2010):
    Code:
    This is NOT an issue with Visual Studio. In Visual C++,
    the "main" method is "main" in ANSI and "wmain" in Unicode.
    By default, Visual Studio sets your project to Unicode, so your
    main function should be renamed "_tmain" (the macro _tmain
    automatically adjusts to "wmain" or "main" depending on 
    whether the project is set to use Unicode).
    So in the end I changed the character set under General in the project properties to "Use Multi-Byte Character Set". This way I can use the standard main syntax instead of _tmain that may not be pressent in other compilers.

    Thanks for all your help.

    The code I have right now:
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <vector>
    
    int main(int argc, char** argv) {
    
       if (argc < 2) {
          // There are no file specified:
          std::cerr << "n  Please, specify the file.n";
          return 1;
       }
    
       // Open the file:
       std::ifstream in (argv[1]);
    
       if (!in.is_open()) {
          // If the file is not open show an error through stderr:
          std::cerr << "n  Failed to open the input file.n";
          return 1;
       }
    
       std::string line;
       std::vector<std::string> contents;
    
       // Discard first line:
       getline(in, line);
       // Fill the vector with the resting lines:
       while (getline(in, line)) {
          contents.push_back(line);
       }
       // Close the file:
       in.close();
    
       // Open the file in write mode:
       std::ofstream out (argv[1], std::ios::trunc);
    
       if (!out.is_open()) {
          // Something bad happened:
          std::cerr << "n  Failed to open the input file.n";
          return 1;
       }
    
       // An iterator for the vector:
       std::vector<std::string>::iterator current = contents.begin();
    
       // Iterate in the vector to fill the output file:
       for (current; current != contents.end(); current++) {
          out << *current << std::endl;
       }
    
       // Finally close the output file and flush to disk:
       out.close();
       return 0;
    }
    Last edited by Francisco; 06-23-2009 at 01:32 PM. Reason: Corrected the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Very strange error...
    By Blackroot in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 01:37 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM