Jump to content

iWearKiltz

Member
  • Posts

    442
  • Joined

  • Last visited

Everything posted by iWearKiltz

  1. For some reason it's missing out the first character of some lines (and i can't see any consistency)
  2. yeah, for some reason i scanned over that and only realised after posting that comment, sorry! i've solved it anyways by just putting in a boolean style identifier which once EOF is reached, a new line is put in. thanks for all your help
  3. this is fantastic, thanks for your help. i've met gets before, but never fgets. it's always good to expand my knowledge with this kind of stuff [when using the same file for file 1 and file 2 - so its just repeated text file of itself] the only issue I'm having right now (which as soon as I've clicked post I'm going to attempt to resolve) is that the last line of both text files is printed on the same line (as the code will see EOF rather then \n and so won't put it on a new line)
  4. but then that uses strings and stuff, and yeah... was trying to avoid that because I'm not the best at strings, but if that is the only way (or better way) then i will do it
  5. Hey guys, I'm working on an assignment (this is part 2 of it for anyone that has seen me post in here before). The assignment is to have two files merged line by line: file 1 line 1 file 2 line 1 file 1 line 2 file 2 line 2 etc I've got it to work for a file which has short lines (reading in text files, the lines can go on for quite a while), but then it incorrectly enters data from file 2 (misses starting character and also doesn't finish first line and then the loop doesn't continue to EOF). When I switch around so that file 2 is read first, it only enters in up to a point (first line of this file is a long line, multiple page scrolls). heres some screenshots: heres the code heres the test files: Please help !
  6. hold up. just tried this //TESTrewind(file_in);int wordcount = 0, letter = 0;char word[30];while ((f = fgetc(file_in)) != EOF){if (f == '.' || f == '=' || f == '-' || f == '_'){f = ' ';}if (f != ' ' && f != '\n'){if (isalpha(f)){word[letter] = tolower(f);letter++;}}else if ((f == ' ' || f == '\n') && letter > 0){letter = 0; wordcount++;word[0] = '\0';}}if (letter > 0)wordcount++;//TESTprintf("printing TEST WORD: %d\n", wordcount); where that last if statement checks if there are letters in the string... anyone see any issues with this?
  7. ahh yeah, that was edited in, didn't see it till now. cheers! this may have solved my problem, look further down yeah that is really useful to know (this stuff just was not taught to me and the more i know the better i'll be !) okay so using JamieF's link I've got this code now //TESTrewind(file_in);int wordcount = 1, letter = 0;char word[30];while ((f = fgetc(file_in)) != EOF){if (f == '.' || f == '=' || f == '-' || f == '_'){f = ' ';}if (f != ' ' && f != '\n'){if (isalpha(f)){word[letter] = tolower(f);letter++;}}else if ((f == ' ' || f == '\n') && letter > 0){letter = 0; wordcount++;word[0] = '\0';}}//TESTprintf("printing TEST WORD: %d\n", wordcount); However I must put in wordcount=1 if the EOF is at the end of a word.. (the program doesn't see EOF as a 'white space character' so due to the fact it being EOF, it skips everything and doesn't add the additional word... any ammendments (other then wordcount=1, because if the last character of the file is a space then we have one extra word now..) thanks
  8. okay but i get 62 words (instead of 30... how have i done this lmao) code:
  9. ammeneded reply: //working out numbers of words for file 1, via 'boolean' with lastcharspace (1 true, 0 false)int lastcharspace = 0;long wc = 0;rewind(file_in);while ((f=fgetc(file_in)) !=EOF){if (f != ' ' && lastcharspace == 0){lastcharspace == 0;}else if (f == ' ' && lastcharspace == 0){lastcharspace = 1;wc++;}else if (f != ' ' && lastcharspace == 1)lastcharspace = 0;else if (f == ' ' && lastcharspace == 1)lastcharspace = 1;elseprintf("there was an error");}printf("The number of words is: %ld \n\n", wc);
  10. working on a solution right now, two ticks
  11. Okay so I implemented this and its a great idea, so heres my sample code: The issue is that in my test file, i've got this: Note the load of spaces on the line 'good to hear.....' So obviously the program is reading those spaces as a bunch of words.. any amendments you can think of? i've been playing around with the if statement but to no avail proof that it doesn't work: thanks again
  12. interesting idea actually, means i get to avoid a 2 dimensional array. any idea how to implement? have two variables and increment both by one each loop, (having one start at 0, other at 1) and have an if loop in a while statement? thats my idea right now, have any better ones?
  13. Hi guys So for part of an assignment i've got to get the word count of a file. I've done characters and lines.. Words are a bit different though it's not like you can count the number of spaces and add one, because that just counts spaces (i.e if there is more then one space in-between words then it will count that as two words when actually it should only be counting one word). So i thought to my self a two dimensional array (string?). Using microsofts secure version of things (_s, if you don't know what this is then just say without the secure version and ill try and work it out), i've tried multiple ways, but my lecturers just have not covered this material (nor half the other stuff i've done for this assignment.. ) was wondering if you guys could help //Places file 1 into an arrayrewind(file_in); //THIS IS ALL A TEST, CHECK FOR PRINTFwhile ((f=fgetc(file_in)) !=EOF){if (isupper(f) || islower(f)){arrayfile1[q][p] = f;p++;}else{arrayfile1[q][p] = '\0';q++;}}
  14. me and you should become friends... or something similar you've solved two of my problems now... have you done a c programming course/module?
  15. Microsoft version a lot of this is using syntax's which i haven't learnt/seen before.. thanks for showing me these, but i don't think i can use these as this is a uni assignment, so i can only really use what they've taught me... (though they haven't ever shown me how to put a string into a fopen_s yet hmm, still not doing it both screenshots show that a static address will show up the file as opened, but a user defined won't work...
  16. microsoft being microsoft decided to make things complicated by introducing a 'safer' version of fopen (and scanf etc) by putting '_s' on the end of them. This changes the format of the arguments and generally makes less sense (or at least to me) in comparison to the standard fopen. now there are ways to get around the _s bit, but as this is for a university assignment, I'm trying to avoid that due to the fact that i suspect that they want me to use the more 'secure' fopen variety... thanks for your help though
  17. hi guys, you're all so smart so I'm back again to ask where I'm going wrong (cause i really don't know and I've searched the web and haven't found anything thus far) so i've opened a pre-defined file using fopen_s. I need to open a user-defined string. will not work right now D: check it out for me please ? code (some of which is a template for later things):
  18. mfw thank you so much, trying to program the rest of the assignment around this issue was hard... now its rectified you've made my life a thousand times easier!
  19. Hey, not really sure what details you will want to know but I'm coding in C using microsoft visual studio (this is running on an emulated version of windows 8.1 - using a macbook pro with parallels desktop 10 to run both mac/windows at the same time). okay so my issue is that i cannot open a file, i have an assignment where i need to open two, so I'm starting with opening one for now. i've created a document called initial1.txt heres my code, someone helps me cause I'm confused as heck! //This program is made for the purpose of text file analysis and merging of text files //This program was first created 3/12/2014 //Made by Laurence Potter LP379 #include <stdio.h> #include <stdlib.h> FILE *file_in; int main(void) { errno_t err; char ccount[25]; err = fopen_s(&file_in, "\\psf\Home\Documents\Visual Studio 2013\Projects\MiniProject\MiniProject\Debug\initial1.txt", "r"); if (err == 0) { printf("The file 'initial1' was opened\n"); } else { printf("The file 'initial1' was not opened\n"); } fscanf_s(file_in, "%s", ccount); printf("%s", ccount); fclose(file_in); return 0; }
  20. right okay thank you very much for sending this to me given me something to think about might just have to find an X1 headset today through black friday!
×