Thread: c programing file scanning

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    18

    c programing file scanning

    how do i scan a name,number,number,status from a file thats look like this:
    Big Little Lies,355,7,Y
    Riverdale,401,1,N
    The X-Files,402,3,Y
    Dark,444,2,Y

    how do i ingore all the "," without scanning them?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,508
    One approach is to use a CSV library since your input appears to be in a CSV format.

    However, if the format is particularly simple, in particular if you don't have to worry about quoted fields and hence about commas embedded into the values, then it instead of a library, you could just read line by line (e.g., using fgets if the maximum line length is known, or the non-standard but POSIX standard getline if it is unknown) and use strtok to separate the fields by comma.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    836
    If you don't need it to be particularly robust and you never have commas in the first string then the simplest way is this:
    Code:
    #include <stdio.h>
     
    int main() {
        FILE *fin = fopen("yourfile", "r");
     
        char name[100], status;
        int a, b;
        while (fscanf(fin, " %99[^,] ,%d ,%d , %c", name, &a, &b, &status) == 4)
            printf("%-20s | %3d | %3d | %c\n", name, a, b, status);
     
        fclose(fin);
        return 0;
    }
    Note that the spaces in the fscanf format are important since they cause fscanf to skip whitespace, if any, at those points. The %d format spec automatically skips leading whitespace so a space is not needed before them, although it wouldn't hurt.

    The %[^,] format spec says to read characters until a comma is encountered. The space before this one is definitely needed as it causes fscanf to skip the newline left by the previous line.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  4. #4
    Registered User
    Join Date
    Dec 2019
    Posts
    18
    Code:
    printfbothfiles(FILE * f1,FILE* f2)
    {
        char sname[22], sid;
        int snum, sstatus;
    
    
        f1 = fopen("s.txt", "r");
        f2 = fopen("n.txt", "r");
        printf("%d---%d", f1, f2);
    
        while (fscanf(f1, "% 99[^, ], %d, %d, %c\n", sname, &snum, &sstatus, &sid) == 4)
            printf("niiice\n");
    
    
    }
    
    void main()
    {
    FILE* f1;
    FILE* f2;
    printfbothfiles(&f1, &f2);
    }
    the print if f1 and f2 in a larege number not a NULL ! and didnt enter the while loop,.

  5. #5
    Registered User
    Join Date
    Dec 2019
    Posts
    18
    i have the #include <stdio.h>

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    836
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
     
    void trimRight(char *str) {
        char *end = str + strlen(str);
        while (end > str && isspace(*--end))
            ;
        end[1] = '\0';
    }
     
    void printFile(const char *filename) {
        FILE *f = fopen(filename, "r");
        if (!f) {
            fprintf(stderr, "Cannot open file: %s\n", filename);
            exit(EXIT_FAILURE);
        }
      
        char sname[100], sid;
        int snum, sstatus;
        while (fscanf(f, " %99[^,] ,%d ,%d , %c",
               sname, &snum, &sstatus, &sid) == 4) {
            trimRight(sname);
            printf("%s %d %d %c\n", sname, snum, sstatus, sid);
        }
    }
      
    int main() {
        printFile("s.txt");
        printFile("n.txt");
        return 0;
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    701
    By the way, main() returns int, not void. Some 30-plus-year-old compilers like Turbo-C let you get away with it, but it's non-standard.

  8. #8
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    240

    Post

    strtok to separate the fields by comma.
    quick example...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define file_one "data.txt"
    
    char *line_data; int line_length;
    
    void commaSwap(char *filename) {
        FILE *fp = fopen(filename, "r");
        while(getline(&line_data, &line_length, fp) != -1) {
            char *token = strtok(line_data, ","); 
            while (token != NULL) { 
                printf(" %s", token); 
                token = strtok(NULL, ","); 
            };
        };
        fclose(fp);
    };
    
    int main(int argv, char *args[]) {
        commaSwap(file_one);
        return 0;
    };
    Last edited by Structure; 01-08-2020 at 01:01 PM.
    "without goto we would be wtf'd"

  9. #9
    Registered User
    Join Date
    Dec 2019
    Posts
    18
    i just have done the scan
    Code:
    int unifyDatabase(FILE* f1, FILE* f2)
    {
        FILE* f3;
        char name[22], sid;
        int i, num, status, fur1, fur2;
    
      
    
    
        f3 = fopen("transistor.txt", "w");
        for (i = 0; fur1 != EOF; i++)
        {
            fur1 = fscanf(f1, " %21[^,] ,%d ,%d , %c", same, &sum, &status, &sid);
            fprintf(f3, "%s,%d,%d,%c\n", name, num, status, sid);
        }
    
    
        fur2 = fscanf(f2, " %21[^,] ,%d ,%d , %c", same, &sum, &status, &sid);
    
    
        //return unifydone;
    }
    what i need to do now is to print all of the lines to a new file named "transistor.txt" just if sid='Y' and
    if the same name and both have sid='N' dont print any
    if the same name one with sid ='Y' and one wiht sid ='N' print the one with Y
    and if both have sid='Y' print the one with higher valu of status.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,677
    Are your two input files line for line identical as far as the names go?
    Eg.
    Code:
    Big Little Lies,355,7,Y
    Riverdale,401,1,N
    The X-Files,402,3,Y
    Dark,444,2,Y
    
    Big Little Lies,55,7,N
    Riverdale,41,1,N
    The X-Files,42,3,N
    Dark,44,2,Y
    Then you can read both files together with something along the lines of
    Code:
        fur1 = fscanf(f1, " %21[^,] ,%d ,%d , %c", name1, &sum1, &status1, &sid1);
        fur2 = fscanf(f2, " %21[^,] ,%d ,%d , %c", name2, &sum2, &status2, &sid2);
        if ( ... ) {
            fprintf(f3, "%s,%d,%d,%c\n", name1, num1, status1, sid1);
        }
    If on the other hand, you have different name order between the files, or one file is a subset of the other, then you probably need to read the whole of the first file into memory, then read the 2nd file one line at a time and do some kind of search.
    Code:
    Big Little Lies,355,7,Y
    Riverdale,401,1,N
    The X-Files,402,3,Y
    Dark,444,2,Y
    
    Riverdale,41,1,N
    Big Little Lies,55,7,N
    Dark,44,2,Y
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Dec 2019
    Posts
    18
    how i can do this part withou printing the same line twice or not printing some lines ?
    Code:
    fur1 = fscanf(f1, " %21[^,] ,%d ,%d , %c", name1, &sum1, &status1, &sid1);
    fur2 = fscanf(f2, " %21[^,] ,%d ,%d , %c", name2, &sum2, &status2, &sid2);
    if ( ... ) {
        fprintf(f3, "%s,%d,%d,%c\n", name1, num1, status1, sid1);
    }
    

  12. #12
    Registered User
    Join Date
    Dec 2019
    Posts
    18
    Code:
    int unifyDatabase(FILE* f1, FILE* f2)
    {
    	FILE* f3;
    	char sname1[22], sid1;
    	int i, snum1, sstatus1, fur1=4, fur2 = 4;
    	char sname2[22], sid2;
    	int snum2, sstatus2;
    
    
    	f3 = fopen("series.txt", "w");
    
    
    	do {
    		fur2 = fscanf(f2, " %21[^,] ,%d ,%d , %c", sname2, &snum2, &sstatus2, &sid2);
    		for (i = 0; fur1 != EOF; i++)
    		{
    			fur1 = fscanf(f1, " %21[^,] ,%d ,%d , %c", sname1, &snum1, &sstatus1, &sid1);
    			fprintf(f3, "%s,%d,%d,%c\n", sname1, snum1, sstatus1, sid1);
    			if (strcmp(sname2, sname1) != 0)
    			{
    				if (snum2 > snum1)
    				{
    					fprintf(f3, "%s,%d,%d,%c\n", sname2, snum2, sstatus2, sid2);
    					//fprintf(f3, "%s,%d,%d,%c\n", sname1, snum1, sstatus1, sid1);
    					fur1 = EOF;
    				}
    				else
    				{
    				fprintf(f3, "%s,%d,%d,%c\n", sname1, snum1, sstatus1, sid1);
    				//fprintf(f3, "%s,%d,%d,%c\n", sname2, snum2, sstatus2, sid2);
    				fur1 = EOF;
    				}
    			}
    			else
    			{
    				if (sid2 == 'N'&&sid1 == 'N')
    					continue;
    				else if (sid2 == 'Y'&&sid1 == 'N')
    				{
    					fprintf(f3, "%s,%d,%d,%c\n", sname2, snum2, sstatus2, sid2);
    					fur1 = EOF;
    				}
    				else if (sid2 == 'N'&&sid1 == 'Y')
    				{
    					fprintf(f3, "%s,%d,%d,%c\n", sname1, snum1, sstatus1, sid1);
    					fur1 = EOF;
    				}
    				else if (sid2 == 'Y'&&sid1 == 'Y')
    					if (sstatus2 >= sstatus1)
    					{
    						fprintf(f3, "%s,%d,%d,%c\n", sname2, snum2, sstatus2, sid2);
    						fur1 = EOF;
    					}
    					else
    					{
    						fprintf(f3, "%s,%d,%d,%c\n", sname1, snum1, sstatus1, sid1);
    						fur1 = EOF;
    					}
    			}
    		}
    	} while (fur1 != EOF && fur2 != EOF);
    
    
    	fclose(f3);
    	return unifydone;
    }
    this is what i have done but it dont print all what i want ..

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,677
    Tell us more about the format of your two input files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Dec 2019
    Posts
    18
    first file:

    The Crown,2,3,Y
    Stranger Things,7,3,N
    Black Mirror,10,2,Y
    Friends,12,25,N
    House of Cards,22,2,Y
    Euphoria,35,5,Y
    The Boys,36,2,N
    Bodyguard,67,12,N
    Killing Eve,99,1,Y
    This Is Us,111,3,Y
    The Americans,112,6,Y
    Felicity,123,4,Y
    Stargate SG-1,213,8,N
    Ozark,276,6,Y
    You,313,10,Y
    Big Little Lies,355,2,Y
    Riverdale,401,4,Y
    The X-Files,402,30,N
    Dark,444,2,Y
    Broadchurch,445,3,Y

    second file:
    The Crown,2,1,Y
    Stranger Things,7,2,Y
    Money Heist,8,4,Y
    Black Mirror,10,12,N
    House of Cards,22,6,Y
    Euphoria,35,7,N
    The Boys,36,5,Y
    Chernobyl,40,2,Y
    Bodyguard,67,5,N
    Family Guy,68,7,N
    This Is Us,111,3,Y
    The Americans,112,6,Y
    Felicity,123,4,Y
    Stargate SG-1,213,8,N
    Altered Carbon,242,2,Y
    Ozark,276,6,Y
    You,313,10,Y
    Big Little Lies,355,7,Y
    Riverdale,401,1,N
    The X-Files,402,3,Y
    Dark,444,2,Y
    Broadchurch,445,1,Y
    Atlanta,446,5,N
    Last edited by Salem; 01-10-2020 at 11:21 PM. Reason: Formatting

  15. #15
    Registered User
    Join Date
    Dec 2019
    Posts
    18
    and the output file need to look like this:
    The Crown,2,3
    Stranger Things,7,2
    Money Heist,8,4
    Black Mirror,10,2
    House of Cards,22,6
    Euphoria,35,5
    The Boys,36,5
    Chernobyl,40,2
    Killing Eve,99,1
    This Is Us,111,3
    The Americans,112,6
    Felicity,123,4
    Altered Carbon,242,2
    Ozark,276,6
    You,313,10
    Big Little Lies,355,7
    Riverdale,401,4
    The X-Files,402,3
    Dark,444,2
    Broadchurch,445,3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanning through a file in C
    By haynesmk in forum C Programming
    Replies: 1
    Last Post: 04-17-2015, 02:19 AM
  2. Scanning A File Help
    By hopes death in forum C Programming
    Replies: 4
    Last Post: 12-01-2013, 10:44 PM
  3. C programing help with file precessing
    By sourabhsinha in forum C Programming
    Replies: 4
    Last Post: 09-10-2010, 10:02 PM
  4. OOP & File scanning.
    By epidemic in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2007, 02:46 PM
  5. file scanning?
    By yahn in forum C++ Programming
    Replies: 4
    Last Post: 12-03-2005, 10:27 AM

Tags for this Thread