Thread: Simple File and String Program Not Running

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    3

    Simple File and String Program Not Running

    Hi my code is not running so I ask that someone test this for me and also review if my logic and syntax is correct.

    The assignment is to write a program that statistically computes similarity of C syntax with another program; a same and a different. The one used here is in C language, it's called Battleship.cpp. The program must open a file and read line by line for keywords and then produce statistics.

    The reason my code is not running is the fopen function is failing and it goes to return -1. I am using MS Visual Studio 2013 and there are no compiler errors after turning off deprecation. I do see, however, this error UMEngx86.dll'. Cannot find or open the PDB file. The file being opened is in my source folder.
    Code:
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 #include <stdlib.h> #include <stdio.h> #include <string.h> main() { char line[100]; char *eof; const char lines, blank = " ", comment1 = "//", comment2 = "/*", ints = "int", longs = "long", floats = "float", doubles = "double", chars = "char", ifs = "if", elses = "else", fors = "for", switches = "swtich", semicolons = ";", structs = "struct", arrays = "[", blocks = "{"; int totalBlanks, totalComments, totalComment1, totalComment2, totalInts, totalLongs, totalFloats, totalDoubles, totalChars, totalIfs, totalElses, totalFors, totalSwitches, totalSemicolons, totalStructs, totalArrays, totalBlocks; FILE* inFile = NULL; const char *p = inFile; printf("Opening file Battle_ship.txt"); inFile = fopen(inFile,"Battle_ship.cpp", "r"); if (inFile == NULL) { printf("Could not open file Battle_ship.cpp. \n"); return -1; } fgets(line, 100, inFile); while (!feof(inFile)) { fgets(line, 100, inFile); } while ((p = strstr(p, blank)) != NULL) { p += strlen(" "); totalBlanks++; } while ((p = strstr(p, comment1)) != NULL) { p += strlen("//"); totalComment1++; } while ((p = strstr(p, comment2)) != NULL) { p += strlen("/*"); totalComment2++; } while ((inFile = strstr(p, ints)) != NULL) { p += strlen("int"); totalInts++; } while ((p = strstr(p, longs)) != NULL) { p += strlen("long"); totalLongs++; } while ((p = strstr(p, floats)) != NULL) { p += strlen("float"); totalFloats++; } while ((p = strstr(p, doubles)) != NULL) { p += strlen("double"); totalDoubles++; } while ((p = strstr(p, chars)) != NULL) { p += strlen("char"); totalBlanks++; } while ((p = strstr(p, ifs)) != NULL) { p += strlen("if"); totalIfs++; } while ((p = strstr(p, elses)) != NULL) { p += strlen("else"); totalElses++; } while ((p = strstr(p, fors)) != NULL) { p += strlen("for"); totalFors++; } while ((p = strstr(p, switches)) != NULL) { p += strlen("switch"); totalSwitches++; } while ((p = strstr(p, semicolons)) != NULL) { p += strlen(";"); totalSemicolons++; } while ((p = strstr(p, structs)) != NULL) { p += strlen("struct"); totalStructs++; } while ((p = strstr(p, arrays)) != NULL) { p += strlen("["); totalArrays++; } while ((p = strstr(p, blocks)) != NULL) { p += strlen("["); totalBlocks++; } totalComments = totalComment1 + totalComment2; totalArrays /= 2; totalBlocks /= 2; printf("%d occurences of %c\n", lines, line); printf("%d occurences of %c\n", totalBlanks, blank); printf("%d occurences of total comments", totalComments); printf("%d occurences of %c\n", totalInts, ints ); printf("%d occurences of %c\n", totalLongs, longs); printf("%d occurences of %c\n", totalFloats, floats ); printf("%d occurences of %c\n", totalDoubles, doubles); printf("%d occurences of %c\n", totalChars, chars); printf("%d occurences of %c\n", totalIfs, ifs); printf("%d occurences of %c\n", totalElses, elses); printf("%d occurences of %c\n", totalFors, fors); printf("%d occurences of %c\n", totalSwitches, switches); printf("%d occurences of %c\n", totalSemicolons, semicolons); printf("%d occurences of %c\n", totalStructs, structs); printf("%d occurences of %c\n", totalArrays, arrays); }

  2. #2
    Registered User
    Join Date
    May 2014
    Posts
    121
    Is the Battle_ship.cpp file located in the same directory as your executable?

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    3
    It wasn't. Wow that made a huge difference. Now how do I make the window stop so I can see the stats?

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    May 2014
    Posts
    121
    If you're using Visual Studio then you can select DEBUG->Start Without Debugging to start your program and then the console window should stick around even after your program has exited.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 09-03-2013, 12:20 PM
  2. Replies: 6
    Last Post: 07-20-2007, 09:23 AM
  3. how to create file in the running program
    By skyboy5122 in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2005, 01:52 AM
  4. Error when running a simple program
    By swishiat.com in forum C Programming
    Replies: 11
    Last Post: 12-14-2003, 06:53 AM
  5. Opening file in directory of the running program
    By Hankyaku in forum C++ Programming
    Replies: 6
    Last Post: 10-11-2003, 06:41 AM

Tags for this Thread