C & C++ string confusion
Shawn O'Shea
shawn at eth0.net
Fri Feb 6 13:54:24 EST 2009
I googled "open file c++" in Google and got this page:
http://www.cplusplus.com/doc/tutorial/files.html
There's an fstream include and you cin and cout to it like to do to
stdin/out.
Also, no reason to call out to the shell. All standard file operations
(create/delete/copy/move/rename) are usually available natively in a given
language. This is true of C/C++. From the same site as above, here's info on
the C++ "remove" command:
http://www.cplusplus.com/reference/clibrary/cstdio/remove.html
-Shawn
On Fri, Feb 6, 2009 at 1:23 PM, <bruce.labitt at autoliv.com> wrote:
> Maybe some on the list might know the answer to this... I am trying to
> read n files, one at a time, and appending the data to a different file.
> Since the files are so large, I need to delete each of the n files, once I
> have captured the data.
>
> Why on earth am I doing this? My arrays are too large to fit in memory
> all at once (I used up all 32GB!!) so I have to process each row of the
> matrix separately. (It slows stuff way down...)
>
> I find string manipulation in C to be a bit arcane. This is what I have
> come up with so far. Unfortunately, (maybe fortunately?) the compiler
> does not like my coding. Oh yes, this has to be in a C or C++ dialect.
> (No "I can do this in x lines of your favorite language" comments. :) )
>
> The code will be compiled using g++ on YDL to run on a QS22 (Cell
> Processor) = Linux content :)
>
> /start code snippet
>
> main()
> {
> string filename;
> string shelldelcmd;
> string mydelstr;
> char filenum[4];
> char filenamec[20];
> FILE * fidjj;
>
> shelldelcmd.assign("rm -f ");
>
> for (jj=0; jj<1000; jj++)
> {
> filename.assign("out");
> sprintf(filenum, "%04d", jj); //generate string for file number, like
> "0010"
> filename.append(filenum); // filename = "outxxxx", where xxxx = jj
> filenamec = filename.c_str; // <===== COMPILER DIES HERE
> ==========
>
> fidjj = fopen(filenamec, "rb"); // <===== location of second error
> if (fidjj==NULL) {fputs ("File error, does not exist\n", stderr);
> exit(1);}
>
> fread some stuff...
> fclose(fidjj);
>
> mydelstr.assign(shelldelcmd);
> mydelstr.append(filename);
> mydelstr.append("\n");
>
> cout << "my delete string is : " << mydelstr << endl;
> system(mydelstr); // delete the file I just read... !!!
> fwrite data to a different file...
> }
> }
>
> /end code snippet
>
> Compiler error is: error; incompatible types of assignment of '<unresolved
> overloaded function type>' to 'char[20]'
>
> If I just use the string "filename" instead of "filenamec" in fopen I get
> two errors, first the one in the previous paragraph, and second is:
>
> error: cannot convert 'std::string' to const char * for argument '1' to
> 'FILE * fopen(const char *, const char*)'
>
> If you think I should step away from the keyboard, well, unfortunately
> that is not an option. I have to learn this stuff as I go along... And
> no, I have never taken a class in C++. I barely have the hang of C...
> FWIW, I tried it in C and suffered some string craziness like unexpected
> overwriting. It was ugly... This approach seems cleaner, except I do not
> know how to convert the C++ strings to be able to use ordinary C fopens...
>
> Any tips or insight would be greatly appreciated... (Awesome tips are
> rewarded with beer!)
>
>
> -Bruce
>
> ******************************
> Neither the footer nor anything else in this E-mail is intended to or
> constitutes an <br>electronic signature and/or legally binding agreement in
> the absence of an <br>express statement or Autoliv policy and/or procedure
> to the contrary.<br>This E-mail and any attachments hereto are Autoliv
> property and may contain legally <br>privileged, confidential and/or
> proprietary information.<br>The recipient of this E-mail is prohibited from
> distributing, copying, forwarding or in any way <br>disseminating any
> material contained within this E-mail without prior written <br>permission
> from the author. If you receive this E-mail in error, please <br>immediately
> notify the author and delete this E-mail. Autoliv disclaims all
> <br>responsibility and liability for the consequences of any person who
> fails to <br>abide by the terms herein. <br>
> ******************************
>
> _______________________________________________
> gnhlug-discuss mailing list
> gnhlug-discuss at mail.gnhlug.org
> http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.gnhlug.org/mailman/private/gnhlug-discuss/attachments/20090206/71da1013/attachment.html
More information about the gnhlug-discuss
mailing list