Jump to content

I've spent most of today trying to put together a script to shave a few more days of work off of a project, but unfortunately I seem to be somewhat stuck. I'm trying to get it to read a text file and output a .csv, with everything formatted in the proper order. Instead, I only get the column headers and a ton of blank rows.

 

The code is here if you'd like to give it a look. http://pastebin.com/6nprusaK

You're and your are not the same. Neither are their, there, and they're. Defiantly and Definitely are definitely not the same. Definately and Rediculous are not words, and you should feel bad for misspelling them. If English is your first language, you don't have a learning disorder, and you get any of these wrong, you are making the entire forum slightly dumber by doing so. Please take the extra three seconds to type properly, and have a nice day.

 

 

Link to comment
https://linustechtips.com/topic/182714-quick-c-question/
Share on other sites

Link to post
Share on other sites

*looks at code*

 

Sample text file input? One or two lines is sufficient.

 

  1. ok...you read the file one character at a time. For something small like this, it isn't a problem. But if you start working on multi-megabyte text files, you'll want to read to a buffer (4096-256K chars in size) and then loop on however many characters were read instead of reading the file one character at a time. It's *much* more efficient and not a great deal harder.
  2. Line 56 is checking for EOF incorrectly. the call is feof(list).
  3. Line 59 can lead to an infinite loop. This is possibly why you are only getting to one column. You read through the file, but never check if you've hit EOF. Any file which doesn't have a : after the ; will lead to early program exit/unexpected output. Change from "while (!= ':') {" to "while (!= ':' && !feof(list)) {"
  4. Ah, you also write out one character at a time. Well, if you go to the buffered input style I mentioned, also write your output to a buffer as well.
  5. Line 71 also needs an feof() check
  6. Line 87 creates a memory leak. You lose all pointers to the block of memory pointed at by *new. I think what you are trying to do is reset the struct? If so, just make a function call that sets all members back to their default values and call that instead. No need for *new and *stock.

 

Fix those things and let's go from there. This will likely take a few iterations to get working properly.

Link to comment
https://linustechtips.com/topic/182714-quick-c-question/#findComment-2461484
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×