创建文件中的文件I / O.

时间:2015-05-22 09:28:42

标签: c++ c

for(c='A'; c<='Z'; ++c)
{
     p = fopen("D:\\DriveName.txt", "w+"); 
     fseek( fp, filepointer , SEEK_SET );
     fputs(buffer,fp); 
     fclose(fp); 
     ++filepointer;
}

每当一个循环进一步通过前一个循环字母被擦除但指针向前移动时。帮助我解释为什么会发生这种情况

2 个答案:

答案 0 :(得分:2)

请改为:

p = fopen("D:\\DriveName.txt", "w"); 
for(c='A'; c<='Z'; ++c)
{
    fputs(buffer,fp); 
}
fclose(fp); 

答案 1 :(得分:0)

我认为你过度了。 fputs()用于编写C字符串,并且您希望一次编写一个字符。 fprintf()可能是更好的选择。试试这个:

public class LastModifiedDir {

    public static void main(String[] args) {
        final String filesLocation = "... your directory to check ...";

        Path path = Paths.get(filesLocation);

        List<Path> dirs = new ArrayList<>();

        try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(path)) {
            for (Path entry : dirStream) {
                if (Files.isDirectory(entry, LinkOption.NOFOLLOW_LINKS)) {
                    dirs.add(entry);
                }
            }
            Collections.sort(dirs, ComparatorReverseLastModificationTime());

            System.out.println("recently changed directory: " + dirs.get(0));
        } catch (IOException e) {
            // add your exception handling here
            e.printStackTrace(System.err);
        }
    }

    static Comparator<Path> ComparatorReverseLastModificationTime() {
        return new Comparator<Path>() {

            @Override
            public int compare(Path o1, Path o2) {
                int compareTo = 0;
                try {
                    compareTo = Files.getLastModifiedTime(o1, LinkOption.NOFOLLOW_LINKS).compareTo(Files.getLastModifiedTime(o2, LinkOption.NOFOLLOW_LINKS));
                } catch (IOException ex) {
                    // proper exception handling must go here
                    ex.printStackTrace(System.err);
                }
                return -compareTo;
            }
        };
    }
}